Sealed Classes And Methods

Sealed keyword enables  us to prevent the inheritance of the class or the class members that were virtual.

Sealed Class

A class can be declared sealed by putting keyword sealed before the class definition.
  1. Public sealed class MyClass {}  
Sealed class can never be a base class, so it can never be an abstract class. It cannot be inherited by any other class. If we put a sealed keyword before any method, Indexer, property, event or class then it will negate its virtual acceptance for any further derived class.

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.
  1. Public sealed override int sum(int a, int b) {}  
Let’s understand by C# example

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.
  1. class Program {  
  2.     //Normal Class   
  3.     public class normalClass {  
  4.         public void Display() {  
  5.             Console.WriteLine("This is a normal class which can be further inherited");  
  6.         }  
  7.     }  
  8.     //Normal Derived   
  9.     public class NormalDerived normalClass {  
  10.         // this Derived class can;t inherit BaseClass because it is sealed   
  11.     }  
  12.     static void Main(string[] args) {  
  13.         normalClass obj = new normalClass();  
  14.         obj.Display();  
  15.         Console.ReadLine();  
  16.     }  
  17. }  
Output will be

Sealed Class

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.
  1. class Program {  
  2.     //Normal Class   
  3.     //Sealed Class   
  4.     public sealed class BaseClass {  
  5.         public void Display() {  
  6.             Console.WriteLine("This is a sealed class which can;t be further inherited");  
  7.         }  
  8.     }  
  9.     //Tried to derive Sealed   
  10.     public class SealedDerived BaseClass {  
  11.         // this Derived class can;t inherit BaseClass because it is sealed   
  12.     }  
  13.     static void Main(string[] args) {  
  14.         BaseClass obj = new BaseClass();  
  15.         obj.Display();  
  16.         Console.ReadLine();  
  17.     }  
  18. }  
Now, we have tried to inherit from sealed class but we can’t inherit from sealed class, when we build it,  it will throw an exception.

Sealed Class

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.
  1. public override sealed void MyFunc() {  
  2.     Console.WriteLine("I am a Sealed method");  
  3. }  
  4. Sealed method cannot be overridden in a class  
  5. class SealedMethod {  
  6.     public class BaseClass {  
  7.         public void myFunc() {  
  8.             Console.WriteLine("This is a normal method");  
  9.         }  
  10.     }  
  11.     //Tried to derive Sealed   
  12.     public class SealedDerivedM BaseClass {  
  13.         // this sealed method will now not be overrideen   
  14.         public virtual sealed void myFunc() {  
  15.             Console.WriteLine("This is a SealedDerived method which can't be further inherited");  
  16.         }  
  17.     }  
  18.     public class threeDerived SealedDerivedM {  
  19.         public virtual void myFunc() {  
  20.             Console.WriteLine("This is a sealed class which can't be further inherited");  
  21.         }  
  22.     }  
  23.     static void Main(string[] args) {  
  24.         BaseClass obj = new BaseClass();  
  25.         obj.myFunc();  
  26.         Console.ReadLine();  
  27.     }  
  28. }  
Class threeDerived is inheriting from class SealedDerivedM but the method myFunc is overriding sealed in SealedDerivedM class, so when we run this program, it will give us an error. Let us run this program and see the output, if it gives any.

Sealed Class

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.
Ebook Download
View all
Learn
View all