Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.

Here is an example that illustrates dynamic method dispatch: 

This program creates one superclass called A and two subclasses of it, called B and C. Subclasses B and C override callme( ) declared in A. Inside the main( ) method, objects of type A, B, and C are declared. Also, a reference of type A, called r, is declared. The program then in turn assigns a reference to each type of object to r and uses that reference to invoke callme( ).

As the output shows, the version of callme( ) executed is determined by the type
of object being referred to at the time of the call. Had it been determined by the type of the reference variable, r, you would see three calls to A’s callme( ) method.

Super Keyword

The super keyword in java is a reference variable that is used to refer parent class objects. The keyword “super” came into the picture with the concept of Inheritance. It is majorly used in the following contexts:

1. Use of super with variables: This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM. We can understand it more clearly using this code snippet: 

                               

In the above example, both base class and subclass have a member maxSpeed. We could access maxSpeed of base class in subclass using super keyword.

2. Use of super with methods: This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword. 

                                       

In the above example, we have seen that if we only call method message () then, the current class message () is invoked but with the use of super keyword, message () of superclass could also be invoked.

3. Use of super with constructors: super keyword can also be used to access the parent class constructor. One more important thing is that ‘’super’ can call both parametric as well as non-parametric constructors depending upon the situation. Following is the code snippet to explain the above concept: 

                             

In the above example we have called the superclass constructor using keyword ‘super’ via subclass constructor.