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.
- public class ClassA
- {
- public void MethodA()
- {
- }
- }
- public class ClassB : ClassA
- {
- public virtual void Method1()
- {
- }
- }
- sealed public class ClassA
- {
- public void MethodA()
- {
- }
- }
- public class ClassB : ClassA
- {
- public virtual void Method1()
- {
- }
- }
'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:
- public class ClassA
- {
- public virtual void Method1()
- {
- }
- }
- public class ClassB : ClassA
- {
- public override void Method1()
- {
- }
- }
- public class ClassC : ClassB
- {
- public override void Method1()
- {
- }
- }
- public class ClassD : ClassC
- {
- public override void Method1()
- {
- }
- }
- public class ClassA
- {
- public virtual void Method1()
- {
- }
- }
- public class ClassB : ClassA
- {
- public override void Method1()
- {
- }
- }
- public class ClassC : ClassB
- {
- public sealed override void Method1()
- {
- }
- }
- public class ClassD : ClassC
- {
- public override void Method1()
- {
- }
- }
'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.

Shakti SaxenaPosted Sep 11, 2015, 8:58 AM
Illustrative article. However, a sentence like "A sealed Class can still be instantiated" would leave reader in peace :)
Santhakumar MunuswamyPosted Sep 5, 2015, 4:14 AM
Thanks for nice share
Shakti SaxenaPosted Sep 4, 2015, 9:38 AM
nice explaination :)
Vikram ChaudharyPosted Sep 4, 2015, 3:06 AM
nice article but I think there is typo in "Like classes we cannot apply sealed keyword to methods directly" I think it should be "Like classes we can apply sealed keyword to methods directly" or am I missing something
Rajeesh MenothPosted Sep 4, 2015, 12:57 AM
Nice Share
Shridhar SharmaPosted Sep 3, 2015, 3:26 PM
nice share
Mohammed IbrahimPosted Sep 3, 2015, 11:36 AM
thanks for sharing information...
Vipul MalhotraPosted Sep 3, 2015, 10:41 AM
nice article.Thanks for sharing
Pankaj Kumar ChoudharyPosted Sep 3, 2015, 8:11 AM
Nice Explain Sir..........
RakeshPosted Sep 3, 2015, 5:13 AM
Good
Sibeesh VenuPosted Sep 3, 2015, 4:54 AM
Nice
Karthikeyan KPosted Sep 3, 2015, 1:56 AM
Good one sir...Thanks for sharing