Sealed Classes in Object-Oriented Programming

Sealed class:data/pngGenerally if we create classes we can inherit the properties of that created class in any class without having any restrictions. In some situation we will get requirement like we don’t want to give permission for the users to derive the classes from it or don’t allow users to inherit the properties from particular class in that situations what we can do? 

For that purpose we have keyword called “Sealed” in OOPS. When we defined class with keyword “Sealed” then we don’t have a chance to derive that particular class and we don’t have permission to inherit the properties from that particular class.


public sealed class A{
public void Print(){
Console.WriteLine("Hello A");
}
}
public class B : A{ // B: cannot derived from sealed type A
public void PrintB(){
Console.WriteLine("Hello B");
}
}
public static void Main(string[] args){
B b = new B();
b.Print();
b.PrintB();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s