What is a Sealed Class in C#?
A Sealed Class is a class that does not allow inheritance, which means the Sealed Class will restrict users to inherit a class. A Sealed Class defined by using a sealed keyword, that keyword notifies the compiler like the defined class is sealed so that it cannot be extended and there is no chance of inheriting a specific class. Sealed Class restricts a class derived from a Sealed Class. The main purpose of the sealed class is to stop inheriting the specific class from other classes. In C#, for our code security, we go for a sealed class, in which we can secure overriding particular methods or properties depend on our conditions. A Sealed Class is a class where we cannot derive or create a new class. In other words, the Sealed Class can’t be inherited by other classes and by using a sealed modifier we can also define a class that is declared called Sealed Class.
Syntax
sealed class _className
{
//data members
//methods
}
Code:
sealed class Demo1 {}
class Demo2 : Demo1{} //invalid
To create any class as a sealed class we must use the keyword sealed.
How does Sealed Class Work in C#?
In general, while creating a class with the help of inheritance we inheriting all the methods and properties in any of the classes. By using sealed class, we can restrict access to the classes and its members with the help of a sealed keyword and we can also avoid inheriting the defined classes from other classes. In C#, a sealed class is a class that cannot be inherited by another class but it can be instantiated. Sealed Class often used for security purposes from preventing the derived class from unwanted users. A Sealed Class is for the most part designed to boundary line the extensibility of the classes.
There are several points while working with Sealed Class, they are:
- A sealed class is entirely different from an abstract class.
- Abstract methods cannot be used in a sealed class.
- In the inheritance hierarchy, it must be the bottom-most class.
- Sealed class purposely used to avoid inheritance.
- The sealed keyword used with methods, classes, properties, and instance.
Let’s see the Working Process of Sealed Class:
Code:
using System;
public class DemoClass
{
public static void Main (string[] args)
{
SealedDemo _sealedObject=new SealedDemo();
int result=_sealedObject.Addition(5,5);
Console.WriteLine("Total Value="+result.ToString());
}
} //sealedClass starts here
sealed class SealedDemo
{
public int Addition(int x, int y)
{
return x+y;
}
}
Output:
When the class is defined as sealed, then that class cannot be inherited and also that Sealed Class can’t be used as the base class. Sealed Class is mainly used for the purpose of restricting the inheritance aspect of OOP.
Sealed Methods in C#
Sealed methods are defined as that the method is a parent class and that method cannot be overridden under child class. When the method is declared virtual in a class any of the child classes can override a method:
Example #1
Code:
using System;
public class Sample_Employee
{
protected int Emp_id, Emp_age;
protected string Emp_name, Emp_address;
public virtual void GetEmployeeDetails()
{
Console.WriteLine("EMPLOYEE DETAILS");
Console.WriteLine("EMPLOYEE ID");
Emp_id = int.Parse(Console.ReadLine());
Console.WriteLine("EMPLOYEE NAME");
Emp_name = Console.ReadLine();
Console.WriteLine("EMPLOYEE ADDRESS");
Emp_address = Console.ReadLine();
Console.WriteLine("EMPLOYEE AGE");
Emp_age = int.Parse(Console.ReadLine());
}
public virtual void DisplayEmployeeDetails()
{
Console.WriteLine("\nEMPLOEE DETAILS:");
Console.WriteLine("EMPLOYEE ID : " + Emp_id);
Console.WriteLine("EMPLOYEE NAME : " + Emp_name);
Console.WriteLine("EMPLOYEE ADDRESS :" + Emp_address);
Console.WriteLine("EMPLOYEE AGE : " + Emp_age);
}
}
public sealed class Emp_Manager : Sample_Employee
{
double Bonus, CA;
public override void GetEmployeeDetails()
{
Console.WriteLine("ENTER MANAGER DETAILS:");
Console.WriteLine("ENTER THE ID");
Emp_id = int.Parse(Console.ReadLine());
Console.WriteLine("ENTER THE NAME");
Emp_name = Console.ReadLine();
Console.WriteLine("ENTER THE BONUS");
Bonus = double.Parse(Console.ReadLine());
Console.WriteLine("ENTER THE CA");
CA = Convert.ToDouble(Console.ReadLine());
}
public override void DisplayEmployeeDetails()
{
Console.WriteLine("MANAGER DETAILS");
Console.WriteLine("EMPLOYEE ID: " + Emp_id);
Console.WriteLine("EMPLOYEE NAME: " + Emp_name);
Console.WriteLine("MANAGER BONUS: " + Bonus);
Console.WriteLine("MANAGER CA : " + CA);
}
}
class Program
{
static void Main(string[] args)
{
Emp_Manager m1 = new Emp_Manager ();
m1.GetEmployeeDetails();
m1.DisplayEmployeeDetails();
Console.ReadKey();
}
}
Output:
We can use a sealed modifier on a property or method which overrides the virtual method in the base class, which enables us to allow classes to derive from class and secure developers who using classes from overriding particular virtual properties or methods. There are a few points which we must define sealed class are:
- We never want to override all properties or methods of a class in sub-classes
- There is no need to expand our class functionalities.
Example #2
The main purpose of using a sealed class is to secure the inheritance of a class; we not required any classes to enlarge the functionality of a class. Let’s see the sample program for the sealed class:
Code:
using System;
using System.Text;
namespace test_SealedClass
{
public class SampleProgram1
{
public sealed class TestBaseClass
{
public static void TestDisplay()
{
Console.WriteLine("Here the SealedClass cannot be inherited");
}
}
public class TestDerived : TestBaseClass
{
// here Derived class cannot be inherited because it's a sealed class
}
static void Main(string[] args)
{
TestBaseClass _object = new TestBaseClass();
_object.TestDisplay();
Console.ReadLine();
}
}
}
When you attempt to derive a class from a sealed class compiler throws a fault error.” TestDerived : cannot derive from sealed type TestBaseClass”.
Advantages in Sealed Class
Let’s see the following important points we need to consider about the sealed keyword in c# language:
- For security purposes, a class restricts inheritance if a class is declared as a sealed class.
- For the class and method, C# applies restrictions when using sealed keywords.
- The local variable is not been sealed in C#
- It’s always applicable when using override for the sealed keyword on property or method.
- A sealed modifier avoids a class being inherited.
Conclusion
This article will give you a better view of sealed class and sealed method and explained the Sealed Class and Sealed Methods in C# with few examples. Hope this article helps you with your requirement.
Recommended Articles
This is a guide to Sealed Class in C#. Here we discuss how sealed class work in c#, and methods in the sealed class with advantages. You can also go through our other related articles to learn more –
6 Online Courses | 18 Hands-on Project | 90+ Hours | Verifiable Certificate of Completion
4.6
View Course
Related Courses