Sunday 7 February 2016

Sealed Methods and Classes in C#

Sealed Classes:

In simplest terms, you can't inherit form a sealed class. The sealed keyword literally seals it for further inheritance.

Why would you want a Sealed Class?

Classes should either be designed for inheritance or they should entirely prohibit it. If a class can be derived from, you have to think of more possibilities, methods like Equals become more complicated and consequently documentation gets bulky.

Now, if there is a class in your design that you are sure will never be a base class, you should go ahead and seal it. Not only it would make your program simple but it would also prevent any bugs that might arise due to ambiguity.

Secondly, it offers better performance. Because a sealed class will never be a base class, run time optimization can make calling sealed class methods slightly faster.

Note: A static class can neither be inherited nor instantiated. However, in the case of a sealed class you can create as many instances as you like, you just can't inherit from it.

Look at the code below:

Our class A is sealed and we have successfully created it's two instances, the first one in class B and the second one in Main function.


 class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.print_B();
            
            A a = new A();
            a.printA();

            Console.ReadLine();
        }
    }

    sealed class A
    {
        private int x = 1;
        public void printA()
        {
            Console.WriteLine(x);
        }
    }

    class B
    { 
        public void print_B()
        {
            A a = new A();
            a.printA();
        }
    }

Now let's see what will happen if we will try to inherit class B from class A.


  

We'll get a compilation error stating that derivation from a sealed class is not possible.

Remember: Other classes can't inherit form sealed class whereas a sealed class can inherit from any other class. 
 
Are sealed methods possible?

Well yes, just like a class you can also seal a method with the use of sealed keyword, but you must also use the keyword "override".

Why? Because the intention behind sealing a method is to stop further overriding of it.

In other words, at any level of inheritance, if you want to restrict the next level of derived classes from overriding a virtual method, then create that method by using the keyword sealed along with the keyword override.

Look at the code below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace ConsoleApplication2
{
    class A
    {
        virtual public void  printA()
        {
            Console.WriteLine("In class A");
        }
    }

    class B : A
    {
        sealed override public void printA()
        {
            Console.WriteLine("In class B");
        }
    }

    class C : B
    {
        override public void printA() //Error: cannot override inherited member
        {                             //B.printA() because it is sealed. 

        }
    }
}


The line 21 will throw an error, since it is trying to override a sealed virtual method.

I hope the above article made sense to you and now the idea and functionality of sealed classes and methods is sealed in your mind.

No comments:

Post a Comment