Sealed Class And Methods In C#

In simplest terms, you can't inherit form a sealed class. The sealed keyword literally seals it for further inheritance. Here we will discuss sealed class and methods in C#.

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 it, 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 will it make your program simple but it will 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.

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         B b = new B();  
  6.         b.print_B();  
  7.           
  8.         A a = new A();  
  9.         a.printA();  
  10.   
  11.         Console.ReadLine();  
  12.     }  
  13. }  
  14.   
  15. sealed class A  
  16. {  
  17.     private int x = 1;  
  18.     public void printA()  
  19.     {  
  20.         Console.WriteLine(x);  
  21.     }  
  22. }  
  23.   
  24. class B  
  25. {   
  26.     public void print_B()  
  27.     {  
  28.         A a = new A();  
  29.         a.printA();  
  30.     }  
  31. }  
Now let's see what will happen if we will try to inherit class B from class A.

Code

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

Remember: Other classes can't inherit from sealed class whereas a sealed class can inherit from any other class.

Are sealed methods possibl?

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. namespace ConsoleApplication2  
  2. {  
  3.     class A  
  4.     {  
  5.         virtual public void  printA()  
  6.         {  
  7.             Console.WriteLine("In class A");  
  8.         }  
  9.     }  
  10.   
  11.     class B : A  
  12.     {  
  13.         sealed override public void printA()  
  14.         {  
  15.             Console.WriteLine("In class B");  
  16.         }  
  17.     }  
  18.   
  19.     class C : B  
  20.     {  
  21.     override public void printA() //Error: cannot override inherited member  
  22.         {                             //B.printA() because it is sealed.   
  23.   
  24.         }  
  25.     }  
  26. }  
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.
 
Read more articles on C# Programming:

Up Next
    Ebook Download
    View all
    Learn
    View all