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

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Jun
22
2013

Understanding the Relationships - Inheritance, Association, Aggregation and Composition in C#

CLICK TO READ FULL ARTICLE

Views - 33577 |Category - C#

Jun
09
2013

What is a constructor in c# and list types of constructors in C#?

CLICK TO READ FULL ARTICLE

Views - 31147 |Category - C#

May
27
2013

Implementing Interface in C# with an example

CLICK TO READ FULL ARTICLE

Views - 65382 |Category - C#

May
21
2013

OOPS Principle - Polymorphism in C# with an example

CLICK TO READ FULL ARTICLE

Views - 100451 |Category - C#

May
17
2013

OOPS Principle - Inheritance in C# with an example

CLICK TO READ FULL ARTICLE

Views - 75591 |Category - C#

 
Author: 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, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

k7afk

 

 
Comments

Posted by Sarmistha nayak on 2016-05-11

thanks very much for best compliment.

Posted by Suresh kumar behera on 2016-04-10

When a class is sealed how it will be inherited

Posted by Himapushpa on 2015-08-05

Explain how sealed keywords works?

Posted by Narendra on 2014-08-08

then What is the difference between Inheriting a class and instantiating a class

Posted 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. { } }

Posted by Rangababu on 2013-10-21

Can explain in scenario we will use sealed classes with example

Posted 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.

Posted 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

Posted by Raja on 2013-09-27

How does sealed prevent inheriting a class? Explain how sealed keyword works?