Skip to Content
Author's profile photo Former Member

REDEFINITION of methods in sub class

 

Using REDEFINITION addition, method implemented in superclass can be reimplemented in subclasses.

When method is called with subclass object reference then method which is redefined in subclass will be called.

Static methods and constructor can not be redefined.

Interface of the method can not be changed in the redefinition of the method.

   

  • Define main class

  MainClass_Defn.PNG

  • Define sub class.Here method mainclass_method is redefined.Addition of FINAL will restrict furter redefinition of method in subclasses.

SubClass_Defn.PNG

  • Implementation of main class.

MainClass_Main.PNG

  • Implementation of sub class.

SubClass_Impl.PNG

  • Create the object and Call methods

Program_main.PNG

 

Output of the above example will be as below.

Output.PNG

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.