Hello All, We are going to start new batch from next week. message/call or mail us for more details.

13 September 2012

Sealed classes and Sealed methods in C#.NET


Sealed classes and Sealed methods in C#.NET



Sealed Classes


When you want to restrict your classes from being inherited by others you can create the class as sealed class.

To create a class as sealed class, create the class using the keyword sealed.

[Access Modifier] sealed class classname
{
}

Sealed Methods 
The virtual nature of a method persists for any number of levels of inheritance.

For example there is a class “A” that contains a virtual method “M1”. Another class “B” is inherited from “A” and another class “C” is inherited from “B”. In this situation class “C” can override the method “M1” regardless of whether class “B” overrides the method “M1”.

At any level of inheritance, if you want to restrict next level of derived classes from overriding a virtual method, then create the method using the keyword sealed along with the keywordoverride.

[Access Modifier] sealed override returntype methodname([Params])
{
}

No comments:

Post a Comment