Getting Started With Sealed Classes in C#

Background


In this article, we will learn about one of the most reusable object-oriented features of C#, Sealed classes. We will learn about Sealed classes from the basics because I have written this article focusing on the students and the beginners. Before proceeding further, please refer to my previous articles for a better understanding. 

So, let us start from the basics of Sealed classes.

What a Sealed Class is


It is a type of class that cannot be inherited.

The following are some key points: 
  • A Sealed class is created by using the sealed keyword
  • The Access modifiers are not applied upon the sealed class
  • To access the members of the sealed we need to create the object of that class
  • To restrict the class from being inherited, the sealed keyword is used

Example

  1.  public sealed class CustoMerDetails  
  2. {  
  3.     public string AccountIno()  
  4.     {  
  5.         return "Vithal Wadje";  
  6.     }  
  7. }  
In the preceding example, the class is declared using the sealed keyword so that this class and the class member cannot be inherited, in other words, we cannot derive from the sealed class.

These types of classes are very Important when we want to restrict it to Inherit the class that provides the highest level of the security to the code, let us see the example of the sealed class, to demonstrate the sealed class, let us create a simple console application as:
  • Open Visual Studio from Start - - All programs -- Microsoft Visual Studio
  • Then, go to "File" -> "New" -> "Project..." then select the Visual C# -> Windows -> Console application
  • After that, specify the name such as the SealedClass or whatever name you wish and the location of the project and click on the OK button. The new project is created.

Now, open the class file and create the following simple program as given below:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace SealedClass  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             //creating the object of sealed class    
  13.             CustoMerDetails obj = new CustoMerDetails();  
  14.             string Name = obj.AccountIno();  
  15.             //writting output to console    
  16.   
  17.             Console.WriteLine(Name);  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21.   
  22.     public sealed class CustoMerDetails //sealed class    
  23.     {  
  24.         public string AccountIno()  
  25.         {  
  26.             return "Vithal Wadje";  
  27.         }  
  28.     }  

In the preceding program, CustoMerDetails is the sealed class containing a method named AccountIno() that cannot be inherited, so to access the member of the specific sealed class, we need to create the object of that class that we have created into the Main method of the program class.

Now run the application, the output will be:
 


Now, you may think, then what is the difference between sealed and private because both the classes cannot be inherited, let us see the difference between these classes.

Private Vs sealed class


PrivateSealed
Private classes cannot be declared directly inside the namespace.Sealed classes can be declared directly inside the namespace.
We cannot create an instance of a private class.We can create the instance of sealed class.
Private Class members are only accessible within a declared class.Sealed class members are accessible outside the class through object.

Note: For the detailed code, please download the attached Zip file.

Summary


I hope this article is useful for all the students and beginners. If you have any suggestion related to this article, please contact me.

 


Similar Articles