Understanding Sealed Keyword For Classes And Methods In C#

Sealed keyword as the name suggests seals the entity to which it is applied. Here in this article we will take classes and methods into consideration.

Sealed Keyword with Classes

When sealed keyword is applied to a class, then that class cannot be inherited by any child class in future.
In short, it means sealed class cannot be inherited. Let’s see an example for the same.

We will create two classes ClassA and ClassB. ClassB will inherit from ClassA.

  1. public class ClassA  
  2. {  
  3.     public void MethodA()  
  4.     {  
  5.     }  
  6. }  
  7.   
  8. public class ClassB : ClassA  
  9. {  
  10.     public virtual void Method1()  
  11.     {  
  12.     }  
  13. }  
The above code works perfectly and no error is generated at compile time. Now apply sealed keyword to ClassA and see how things changes:
  1. sealed public class ClassA  
  2. {  
  3.     public void MethodA()  
  4.     {  
  5.     }  
  6. }  
  7.   
  8. public class ClassB : ClassA  
  9. {  
  10.     public virtual void Method1()  
  11.     {  
  12.     }  
  13. }  
Please note that blue color of ClassA changed to black when ClassB inherits ClassA. Also while compiling the solution following error will be generated:

'ClassB': cannot derive from sealed type ‘ClassA'

The above shows the working of sealed keyword with class. Class can be sealed for inheritance with the sealed keyword.

Sealed Keyword with Methods


Like classes we cannot apply sealed keyword to methods directly. Sealed keyword in methods is used to stop further overriding of the virtual methods during Inheritance.

Let’s see an example to understand the above statement:
  1. public class ClassA  
  2. {  
  3.     public virtual void Method1()  
  4.     {  
  5.     }  
  6. }  
  7.   
  8. public class ClassB : ClassA  
  9. {  
  10.     public override void Method1()  
  11.     {  
  12.     }  
  13. }  
  14.   
  15. public class ClassC : ClassB  
  16. {  
  17.     public override void Method1()  
  18.     {  
  19.     }  
  20. }  
  21.   
  22. public class ClassD : ClassC  
  23. {  
  24.     public override void Method1()  
  25.     {  
  26.     }  
  27. }  
This code works perfectly. We can see each class overrides virtual method Method1 and no compilation error occurs. Now let’s add sealed keyword while overriding Method1 in ClassC.
  1. public class ClassA  
  2. {  
  3.     public virtual void Method1()  
  4.     {  
  5.     }  
  6. }  
  7.   
  8. public class ClassB : ClassA  
  9. {  
  10.     public override void Method1()  
  11.     {  
  12.     }  
  13. }  
  14.   
  15. public class ClassC : ClassB  
  16. {  
  17.     public sealed override void Method1()  
  18.     {  
  19.     }  
  20. }  
  21.   
  22. public class ClassD : ClassC  
  23. {  
  24.     public override void Method1()  
  25.     {  
  26.     }  
  27. }  
Now after compiling the code, the following error generates:

 

    'ConsoleApplication1.ClassD.Method1()': cannot override inherited member 'ConsoleApplication1.ClassC.Method1()' because it is sealed

The above example and explanation shows the working of sealed keyword with method. Method can be sealed for overriding with the sealed keyword.

I hope these simple examples will help you in understanding the working of Sealed keyword with Classes and Methods.

Happy Coding.


Recommended Free Ebook
Similar Articles