What is Sealed Class and Sealed Method?

jayalaxmi By jayalaxmi, 24th Oct 2014 | Follow this author | RSS Feed
Posted in Wikinut>Jobs>Training

in these articles explain about sealed class and sealed class method

Sealed Class

a class which can’t be inherited by any other class is referred as a Sealed Class. To make a class Sealed we need to apply sealed modifier on it.

Syntax:

sealed class Class1
{
- members
}
class Class2 : Class1 // invalid


Note – a sealed class if required can be consumed from other classes by creating its object.

Sealed Method

A method which cannot be overridden under a child class is referred as a sealed method. By default every method is sealed method because overriding a method is not possible unless it was declared as virtual.
If at all a method is declared as virtual within a class any of its child class in the linear hierarchy has a chance to override the method.

Example

class Class1
public virtual void Display()

class Class2 : Class1
public override void Display()

class Class3 : Class2
public override void Display()

A virtual method of a class can be sealed by its child class so that further overriding of the method by Childs of child class will not be possible.

class Class1
public virtual void Display()

class Class2 : Class1
public sealed override void Display()

class Class3 : Class2
public override void Display() // invalid

Tags

C Sealed Class, Class, Csharp Sealed Class, Method, Net Sealed Class, Sealed Class, Sealed Method

Meet the author

author avatar jayalaxmi
myself jayalaxmi from bangalore and i'm a software engineer i like to share my knowledge with other.

Share this page

moderator Steve Kinsman moderated this page.
If you have any complaints about this content, please let us know

Comments

author avatar AjaySinghChauhan
25th Oct 2014 (#)

very nice post i like it

Reply to this comment

Add a comment
Username
Can't login?
Password