What are sealed classes and sealed methods

In this article, I will try to explain sealed classes and sealed methods in C# language.

What is sealed class and sealed method?

 
In this article, I will try to explain sealed class and sealed method in a very simple way.
 

Sealed Class

 
Sealed class is used to define the inheritance level of a class.
 
The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class.
 
Some points to remember:
  1. A class, which restricts inheritance for security reason is declared sealed class.
  2. Sealed class is the last class in the hierarchy.
  3. Sealed class can be a derived class but can't be a base class.
  4. A sealed class cannot also be an abstract class. Because abstract class has to provide functionality and here we are restricting it to inherit.

Practical demonstration of sealed class

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace sealed_class  
  6. {  
  7.     class Program  
  8.     {  
  9.         public sealed class BaseClass  
  10.         {  
  11.             public void Display()  
  12.             {  
  13.                 Console.WriteLine("This is a sealed class which can;t be further inherited");  
  14.             }  
  15.         }  
  16.         public class Derived : BaseClass  
  17.         {  
  18.             // this Derived class can;t inherit BaseClass because it is sealed  
  19.         }  
  20.         static void Main(string[] args)  
  21.         {  
  22.             BaseClass obj = new BaseClass();  
  23.             obj.Display();  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  

Sealed Methods

 
Sealed method is used to define the overriding level of a virtual method.
 
Sealed keyword is always used with override keyword.
 
Practical demonstration of sealed method

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace sealed_method  
  6. {  
  7.     class Program  
  8.     {  
  9.         public class BaseClass  
  10.         {  
  11.             public virtual void Display()  
  12.             {  
  13.                 Console.WriteLine("Virtual method");  
  14.             }  
  15.         }  
  16.         public class DerivedClass : BaseClass  
  17.         {  
  18.             // Now the display method have been sealed and can;t be overridden  
  19.             public override sealed void Display()  
  20.             {  
  21.                 Console.WriteLine("Sealed method");  
  22.             }  
  23.         }  
  24.         //public class ThirdClass : DerivedClass  
  25.         //{  
  26.         //    public override void Display()  
  27.         //    {  
  28.         //        Console.WriteLine("Here we try again to override display method which is not possible and will give error");  
  29.         //    }  
  30.         //}  
  31.         static void Main(string[] args)  
  32.         {  
  33.             DerivedClass ob1 = new DerivedClass();  
  34.             ob1.Display();  
  35.             Console.ReadLine();  
  36.         }  
  37.     }  
  38. }  

 

Hope this article will give you better view of sealed class and sealed method. Waiting! for your valuable feedback.