Sealed Modifier

Posted on Updated on

Image result for C# dot netSealed Modifier

Sealed modifier when used with a class defines the inheritance level. When sealed is used with a class, they cannot be inherited by other classes. In other words, it prevents the derivation of the class. To make a class sealed, “sealed” keyword is used before the “class” keyword. If “sealed” modifier is used with a method, that method cannot be overloaded.

Features of Sealed Class

1) A class which is to be used for security reason is declared as sealed.
2) A declared as sealed is the last class in the hierarchy.
3) Sealed class cannot be a base class but can be a derived class.
4) Sealed class cannot be declared as abstract.

Class Definition of Sealed class

sealed class SealedClass
{

}

Method definition for Sealed method

public override sealed void SealedMethod()
{

}

Example

class X
{
protected virtual void F() { Console.WriteLine(“X.F”); }
protected virtual void F2() { Console.WriteLine(“X.F2”); }
}

class Y : X
{
sealed protected override void F() { Console.WriteLine(“Y.F”); }
protected override void F2() { Console.WriteLine(“Y.F2”); }
}

class Z : Y
{
// Attempting to override F causes compiler error CS0239.
// protected override void F() { Console.WriteLine(“C.F”); }

// Overriding F2 is allowed.
protected override void F2() { Console.WriteLine(“Z.F2”); }
}

****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s