Saturday, April 18, 2020

Sealed Class in C#

C# Sealed Class

 
In this article, I will discuss how to create and use a sealed class using C#. We will also see why developers use sealed classes in their libraries and code.
 
Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, this class cannot be inherited. 

In C#, the sealed modifier is used to declare a class as sealed. In Visual Basic .NET, NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class, compiler throws an error.

If you have ever noticed, structs are sealed. You cannot derive a class from a struct. 

The following class definition defines a sealed class in C#: 
  1. // Sealed class  
  2. sealed class SealedClass  
  3. {  
  4. }   
In the following code, I create a sealed class SealedClass and use it from Class1. If you run this code, it will work just fine. But if you try to derive a class from the SealedClass, you will get an error.
  1. using System;  
  2. class Class1  
  3. {  
  4.     static void Main(string[] args)  
  5.     {  
  6.         SealedClass sealedCls = new SealedClass();  
  7.         int total = sealedCls.Add(4, 5);  
  8.         Console.WriteLine("Total = " + total.ToString());  
  9.     }  
  10. }  
  11. // Sealed class  
  12. sealed class SealedClass  
  13. {  
  14.     public int Add(int x, int y)  
  15.     {  
  16.         return x + y;  
  17.     }  
  18. }   

MSDN Updated - Sealed Methods and Properties 

You can also use the sealed modifier on a method or a property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent other developers that are using your classes from overriding specific virtual methods and properties.
  1. class X  
  2. {  
  3.     protected virtual void F()  
  4.     {   
  5.         Console.WriteLine("X.F");   
  6.     }  
  7.     protected virtual void F2()  
  8.     {  
  9.         Console.WriteLine("X.F2");   
  10.     }  
  11. }  
  12. class Y : X  
  13. {  
  14.     sealed protected override void F()  
  15.     {  
  16.         Console.WriteLine("Y.F");  
  17.     }  
  18.     protected override void F2()  
  19.     {  
  20.         Console.WriteLine("X.F3");  
  21.     }  
  22. }  
  23. class Z : Y  
  24. {  
  25.     // Attempting to override F causes compiler error CS0239.  
  26.     //   
  27.     protected override void F()  
  28.     {  
  29.          Console.WriteLine("C.F");   
  30.     }  
  31.     // Overriding F2 is allowed.   
  32.     protected override void F2()  
  33.     {  
  34.         Console.WriteLine("Z.F2");   
  35.     }  
  36. }  

Why Sealed Classes?

We just saw how to create and use a sealed class in C#. The main purpose of a sealed class is to take away the inheritance feature from the class users so they cannot derive a class from it. One of the best usage of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace.
The Pens class represents the pens with standard colors. This class has only static members. For example, Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes. The Brushes.Blue represents a brush with blue color.
 
So when you're designing a class library and want to restrict your classes not to be derived by developers, you may want to use sealed classes.

No comments:

Post a Comment