Method Overriding (Cont.)


private Methods Not Being Overridden
Private methods cannot be overridden as they are bonded during compile time. Therefore we can’t even override private methods in a subclass.

Overriding Method Having the Same Return Type (or Subtype)
From Java 5.0 onwards, it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. This phenomena is known as covariant return type.

Invoking Overridden Method from Subclass
We can call parent class method in overriding method using the super keyword.

Subclass.java (invoking overridden method from subclass)
// To demonstrate that overridden method can be called from subclass 
// Driver class
class Subclass {
  public static void main( String[ ] args ) {
    Parent obj = new Child( );
    obj.show( );
  }
}
// Base class 
class Parent {
  void show( ) { System.out.println( "Parent's show( )" ); } 
} 
// Inherited class 
class Child extends Parent { 
  // This method overrides show( ) of Parent. 
  @Override
  void show( ) { 
    super.show( ); 
    System.out.println( "Child's show( )" );
  } 
}
shell> java Subclass           Result:




      “Wonder is the beginning of wisdom.”    
      ― Socrates