Sealed Classes
Sealed Classes are those classes that cannot be inherited by other classes. If we want to prevent a class to be inherited by other class then we make that class as Sealed Class. To create a class as sealed class, create the class using the keyword sealed.Syntax:-
[Access Modifier] sealed class classname
{
//Class
Body
}
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 keyword override.
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 keyword override.
Syntax:-
[Access Modifier] sealed override returntype
methodname ([Params])
{
//Method
Body
}
No comments:
Post a Comment