Understand Sealed Classes in C# with example

About Sealed Modifier ?

Sealed modifier restricts the inheritance feature of an object oriented programming means sealed classes cannot be inherited or implemented further to any classes. To implement sealed classes in programs we need to use "sealed" keyword before the "class" keyword.

Why Sealed Modifier ?

We use sealed modifiers in our programs because:

Sealed Modifier prevents a class from being inherited.

When sealed modifier is applied to a method then it prevents a method from being overloaded in further classes.

sealed class Employee ///Sealed class
    Public sealed void Display() { } /// Sealed Method

Example of Sealed Modifier:

sealed class Employee
    public void DisplayEmployeeDetails(){
Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");

In above the code we have class "Employee"with "sealed" type modifier and simple "DisplayEmployeeDetails" method which displays employee details.

Now let's perform test on sealed type modifier.

To perform this test we will create a main program of console with static main method. Now let's try to inherit the "Employee" class to our main program as shown in below code.


As you can see when we tried to build the application we got following error which states that we cannot derive from sealed type member but we can aggregate the "Employee" class with sealed type modifier in our main method as shown in below code.

So when we try to call "DisplayEmployeeDetails" method through the object "objEmp" we could find "DisplayEmployeeDetails" method as shown in above code.

Conclusion: Sealed member prevent the class for further inheriting. 

So this all about sealed modifiers in c-sharp. If you have any doubts or queries regarding sealed modifiers then kindly let me know through your valuable comments and if you like this article information then share this with your friends and colleagues. Thank You

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more


By Sarmistha nayak on 2016-05-11
thanks very much for best compliment.
By Suresh kumar behera on 2016-04-10
When a class is sealed how it will be inherited
By Himapushpa on 2015-08-05
Explain how sealed keywords works?
By Narendra on 2014-08-08
then What is the difference between Inheriting a class and instantiating a class
By Suman on 2014-04-04
Hi Gurunatha Dogi, How the below code snippet possible? sealed class Employee ///Sealed class { public sealed void Display() { } /// Sealed Method } We can't restrict a method from overloading unless it is override from other class. Check this below snippet . public class Emp { public virtual void Display() { } } public class Employee : Emp///Sealed class { public sealed override void Display()/// Sealed Method { } } public class DevClass : Employee { public override void Display()///this is not possible as the Display method is sealed at Employee class. And if u want have method to be sealed it must be overridable method. { } }
By Rangababu on 2013-10-21
Can explain in scenario we will use sealed classes with example
By Vincent on 2013-10-20
Pretty good explanations. Do you have any other blog that explains the C# class types such as basic and abstract etc. Thank you.
By Narayana Moorthy on 2013-10-16
HI , Please let me know the real time scenario of sealed class and method. Advance thanks. Regarrds, Narayana Moorthy.M
By Raja on 2013-09-27
How does sealed prevent inheriting a class? Explain how sealed keyword works?

Add a Comment