Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Kindly help me to solve it out this problem:

If am declaring a sealed class,it means that I cannot inherit it.
But there may be a case that a sealed class is containing a function with public access specifier.
I just want to know that this function can be inherited or not?????
If not then what is the need to declare it as public and if yes then how is it possible?
Posted
Updated 15-May-10 8:22am
v2

Sealed classes cannot be derived from, but if it is also public with public methods they can be called by any code using an instance of that class.
 
Share this answer
 
You can't "inherit" individual methods. Like Davey said, if it's public, you can access it from outside the class.
 
Share this answer
 
sweety2010 wrote:
If am declaring a sealed class,it means that i cannot inherit it.


which is 100% true
sweety2010 wrote:
a sealed class is containing a function with public access specifier.


sealed class radix
    {
        public void fun()
        {
            Console.WriteLine("Hello");
        }
    }


OK now...
sweety2010 wrote:
I just want to know that this function can be inherited or not?????


No the class is sealed and the function fun() is inside the sealed class radix so u cannot Inherit that

sweety2010 wrote:
if yes then how is ii possible?


Yes it is possible with the help of the code given below
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    sealed class radix
    {
        public void fun()
        {
            Console.WriteLine("Hello");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            radix obj = new radix();
            obj.fun();
        }
    }
}


By creating the object of the sealed class Radix any attempt to inherit radix will cause an error and the compiler will not allow it.

Do rate my answer once you find it useful
Thanks & Regards
Radix :)
 
Share this answer
 
Ah - it's not obvious from your question but are you talking about wrapping a sealed class because you can't inherit it?

If so, then it's dead easy.
C#
public sealed class SealedClass
{
    public int DoSomething()
    {
        // ....
        return 0;
    }
}

C#
public class WrapperClass
{
    // wrapped sealed class
    private SealedClass sealedClass;

    public WrapperClass()
    {
        // create instance of wrapped class
        sealedClass = new SealedClass();
    }

    public int DoSomething()
    {
        // call wrapped class's method and return it's return value
        return sealedClass.DoSomething();
    }
}
 
Share this answer
 
Hum, you are doing some confusion with the concepts you are talking about; here below a little summary on that:


    • when a class is declared as sealed, it means that you cannot inherit from that class, but obviously you should be able to instantiate and use object of that class (the idea on sealed classes is that such classes are complete and ready for use, and that it make no sense to extend them, then you cannot inherit from them);

    • you can only inherit from interfaces and classes, not from methods. What you can do with methods is to override, but to override a method (or a property) you must inherit from the class or interface that expose it;

    • due to the fact that sealed classes cannot be inherited, it's obvious that such classes expose public methods and/or properties. If not they are not useful: you cannot use them by inheritance nor by instantiating an object and making calls on it;

    • the scope of a class (i.e. how the class has been declared: public, protected, private, internal) is related to its visibility and accessibility from other classes and assemblies. For instance, you can declare a class as internal if you want that it will be visible and usable only from within your assembly. Anyway if a class is not nested inside another one, it make no sense to not declare it as public. For instance, a private class nested inside another class can be accessed only from the outer class, but a private class that is a top-level one (i.e. not nested inside another class) couldn't be accessed by anyone!

 
Share this answer
 
You could always write extension methods to add functionality...

(If you're using C# 3.0 or above)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900