What is sealed class and sealed method in C# Programming?

Answer: Sealed class cannot be inherited and sealed method in C# programming cannot be overridden. If we need to stop a method to be overridden or further extension of a class in inheritance hierarchy,  we need to use Sealed method and Sealed class respectively in C# object oriented programming.

Sealed class in C# : A sealed class cannot be inherited. Means, no class can be derived from the sealed class.

For example if we seal class A as below source code. class B cannot be derived from it.


Sealed method in C# : Prevent overriding a method of a class.

This is a method that is declared with the keyword sealed and is always used with combination of override keyword. Derived classes will not be able to override this method as it is sealed for overriding.

C# Program Example: In below source code example class B will seal the X(), so, it cannot be overridden / Implemented in class C and class C will use the same X() of class B.



There is another important technical interview question you should follow that is real life example of sealed class and method in C# object oriented programming.

NOTE: Sealed class and sealed method in C# language is used to stop a developer to extend a class or override a method accidentally. If he do so, C# compiler will flash an error. If he rally want to extend or override he can remove the sealed keyword in C# program.