Method Overriding (Cont.)


Overriding and Abstract Methods
Abstract methods in an interface or abstract class are meant to be overridden in derived concrete classes otherwise compilation error will be thrown.

Overriding and synchronized / strictfp Methods
The presence of synchronized / strictfp modifier with method have no effect on the rules of overriding; i.e., it’s possible that a synchronized/strictfp method can override a non synchronized / strictfp one and vice-versa.

Final Comments
In C++, we need virtual keyword to achieve overriding or runtime polymorphism. In Java, methods are virtual by default. We can have multi-level method-overriding.

Multilevel.java (multi-level overriding)
// A program to demonstrate multi-level overriding 
// Driver class
class Multilevel {
  public static void main( String[ ] args ) {
    Parent obj1 = new GrandChild( );
    obj1.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. 
  void show( ) { System.out.println( "Child's show( )" ); } 
} 
// Inherited class 
class GrandChild extends Child {
  // This method overrides show( ) of Parent. 
  void show( ) { System.out.println( "GrandChild's show( )" ); } 
}
shell> java Multilevel           Result: