Monday 25 February 2013

Sealed Classes and Sealed Methods

  • Sealed Classes: A Class which is defined by using the sealed modifier is a sealed class but if any class is declared as sealed the class cannot be inherited by any other class 
           for example:
           sealed class class1
           class2:class1    //Invalid
Note: We can still consume a sealed class from any other class by creating the object even if inheritance is not possible.
  • Sealed Methods: A method which cannot be overridden under child classes is called as Sealed Methods and by default every method of a class is a sealed method because overriding a child class under parent class is possible only when the method is declared as virtual.
            If a method is declared as virtual under a class, any child class of the class linear hierarchy has a right to override the method. 
       class1
       public virtual void show()
       class2:class1    
       public override void show()
       class3:class2
       public override void show()
Note: In the above case even if class2 does not override the method then also class3 can override the method.

No comments:

Post a Comment