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, 4:58 AM EDT
Illustrative article. However, a sentence like "A sealed Class can still be instantiated" would leave reader in peace :)
Santhakumar MunuswamyPosted Sep 5, 2015, 12:14 AM EDT
Thanks for nice share
Shakti SaxenaPosted Sep 4, 2015, 5:38 AM EDT
nice explaination :)
Vikram ChaudharyPosted Sep 3, 2015, 11:06 PM EDT
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 3, 2015, 8:57 PM EDT
Nice Share
Shridhar SharmaPosted Sep 3, 2015, 11:26 AM EDT
nice share
Mohammed IbrahimPosted Sep 3, 2015, 7:36 AM EDT
thanks for sharing information...
Vipul MalhotraPosted Sep 3, 2015, 6:41 AM EDT
nice article.Thanks for sharing
Pankaj Kumar ChoudharyTOP 100Posted Sep 3, 2015, 4:11 AM EDT
Nice Explain Sir..........
RakeshPosted Sep 3, 2015, 1:13 AM EDT
Good
Sibeesh VenuTOP 100Posted Sep 3, 2015, 12:54 AM EDT
Nice
Karthikeyan KPosted Sep 2, 2015, 9:56 PM EDT
Good one sir...Thanks for sharing