Sealed Class
A class can be declared sealed by putting keyword sealed before the class definition.
- Public sealed class MyClass {}
The last class in the hierarchy is the sealed class. Sealed class cannot be a base class nor can it be abstract class.
The virtual nature of a method persists for any number of levels of inheritance. e.g
A class “A” contains a virtual method “VM”. Another Class “B” is inherited from A and one more class “C” is inherited from B. In this type of situation, class “C” can override the method “VM” regardless of whether class “B” overrides the Method “VM”. Now, if at any level of inheritance, we want to restrict next derived class from inheriting the virtual method “VM”, then we will create the method, using the keyword sealed.
- Public sealed override int sum(int a, int b) {}
In the example given below, we have a base class as a normal class and derived class as NormalDerived. If we inherit NormalDerived from normal class, there will be no problem executing the program.
- class Program {
- //Normal Class
- public class normalClass {
- public void Display() {
- Console.WriteLine("This is a normal class which can be further inherited");
- }
- }
- //Normal Derived
- public class NormalDerived normalClass {
- // this Derived class can;t inherit BaseClass because it is sealed
- }
- static void Main(string[] args) {
- normalClass obj = new normalClass();
- obj.Display();
- Console.ReadLine();
- }
- }

Now, if we see same for the sealed class, we will get the error. For sealed class, we have written a program, as shown below.
- class Program {
- //Normal Class
- //Sealed Class
- public sealed class BaseClass {
- public void Display() {
- Console.WriteLine("This is a sealed class which can;t be further inherited");
- }
- }
- //Tried to derive Sealed
- public class SealedDerived BaseClass {
- // this Derived class can;t inherit BaseClass because it is sealed
- }
- static void Main(string[] args) {
- BaseClass obj = new BaseClass();
- obj.Display();
- Console.ReadLine();
- }
- }

Error has occurred during the running of the program and the class cannot inherit from sealed class.
Sealed Method
Sealed method is used to control the overriding nature of the virtual method. Sealed method can be defined by using sealed keyword before return type of method.
The example is given below.
- public override sealed void MyFunc() {
- Console.WriteLine("I am a Sealed method");
- }
- Sealed method cannot be overridden in a class
- class SealedMethod {
- public class BaseClass {
- public void myFunc() {
- Console.WriteLine("This is a normal method");
- }
- }
- //Tried to derive Sealed
- public class SealedDerivedM BaseClass {
- // this sealed method will now not be overrideen
- public virtual sealed void myFunc() {
- Console.WriteLine("This is a SealedDerived method which can't be further inherited");
- }
- }
- public class threeDerived SealedDerivedM {
- public virtual void myFunc() {
- Console.WriteLine("This is a sealed class which can't be further inherited");
- }
- }
- static void Main(string[] args) {
- BaseClass obj = new BaseClass();
- obj.myFunc();
- Console.ReadLine();
- }
- }

It has thrown a sealed method error that it can’t be overridden. Sealed keyword is used before a method to stop it from being overridden in the derived class.
Please provide your valuable feedback.

Join the conversation! Your thoughts help the community grow.