Sealed Class Explained In C#

Introduction

 
In this article, I will explain how to create and use a sealed class in C# with some examples. A Sealed class is created using the “Sealed” keyword in C#. The Sealed Class is used for the restricted inheritance feature of object-oriented programming. Once the sealed class has been defined, this class cannot be inherited, but we can create an instance to call the function. The sealed keyword is used to declare a class as sealed. A sealed class is used for the inheritance level of a class. This class cannot be inherited by any other class. A sealed class can be a derived class but cannot be a base class and also cannot be an abstract class since the abstract class must provide functionality. I have written this article focusing on students and beginners.
 
Key Points
  1. A Sealed class is created by using the "sealed" Modifier.
  2. To access the members of the sealed class we can instance create of the class.
  3. Sealed class is defined this class cannot be inherited.
  4. The sealed class is the inheritance level of a class.

How to create a Sealed Class

 
Step 1
  1. Create a class Name Common (or) keep as it as you want.
  2. Create the Sealed class by using “Sealed” Keyword.
  3. It will show the window given below in Fig 1.
Sealed Class Explained In C# 
Fig: 1
 
Syntax
  1. sealed class Common  
  2. {  
  3.    
  4. }  

Step 2 - How we can use sealed class

In this following code, create a sealed class using the “Sealed” Keyword and use it from the Common class. Then, create a new method in the same class and implement the methods. Call the class named test and run this code. We will get an error.
  1. I created a sealed class using the “Sealed” modifier then create the three methods in sealed class and implement the methods.
  2. Create a new class name as test then sealed class instance create to call function.
Write the following code. Please download the “sealed class" .zip file.
 
Coding
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace SealedClassTest  
  8. {  
  9.    public sealed class Common  
  10.    {  
  11.        public int Add(int x,int y)  
  12.        {  
  13.             return (x + y);  
  14.        }  
  15.        public int Sub(int x,int y)  
  16.        {  
  17.             return (x - y);  
  18.        }  
  19.        public int Multiply(int x,int y)  
  20.        {  
  21.             return (x * y);  
  22.        }  
  23.   
  24.    }  
  25.     public class Test   
  26.     {  
  27.         static void MethodName(string[] args)  
  28.         {  
  29.             Common Ocommon = new Common();  
  30.             //Add  
  31.             int Value=  Ocommon.Add(10, 25);  
  32.             Console.WriteLine("Your Value is" + Value.ToString());  
  33.             //Sub  
  34.             int subValue = Ocommon.Sub(25, 10);  
  35.             Console.WriteLine("Your Value is" + subValue.ToString());  
  36.             //Multiply  
  37.             int mulValue = Ocommon.Multiply(10, 2);  
  38.             Console.WriteLine("Your Value is" + mulValue.ToString());  
  39.          }  
  40.   
  41.          
  42.     }  
  43.  }  
Step 3 - Finding the Error
  1. Inheritance type shows an error message.
  2. The error occurs if a sealed class is specified as the base class of another class.
  3. It will show in the Screenshot given below.
Sealed Class Explained In C# 
Fig: 2
 

Difference between static class and sealed class

 
 Sealed class
 Static class
 Sealed class can instance create to call function  static class is we cannot instance create to call function
 The sealed class contains static as well as non-static members  The static class is we can access to static members only
 Sealed class can't be never inherited  static class is we can inherit by other static class
 Sealed class is we can't use to base class  static class is we can use to base class
 
Note

If you want more information about static class, please refer to the previous articles
 

Summary

 
In this article, I discussed a Sealed class in C#. I have written this article focusing on beginners and students.

Similar Articles