Saturday, February 27, 2016

OOPS : Sealed Classes

Sealed Classes

If class is declared as sealed using sealed modifier it cant be inherited by any other class but can be still be consume by creating its object

Seale class class1
{
members
}

Class Class2:class1//invalid

Sealed method
Method which cant be overridden under child classes is known as sealed methods by default every method in class is sealed because overridden of method is not possible unless it is declared as virtual

If method was declared as virtual in class any of its child class hierarchy has a chance to override method

Class1
Public virtual void show ()

Classs2:class1
Public override void show ()


Classs3:class2
Public override void show ()

If at all child class wants to restrict its child classes not to override virtual methods of its parent it can restrict it by declaring methods as sealed while overriding it

Class1
Public virtual void show ()

Class2:class1
Public sealed override void show ()

Classs3:class2
Public override void show ()//invalid

No comments:

Post a Comment