Method Overriding and Runtime Polymorphism in Java

Method Overriding

 

When a subclass method has the same name and type signature as its superclass method, then the method is subclass is said to override the corresponding method in the superclass.

 

When a overridden method is called from subclass, it always refers to the subclass version of the method… the superclass version of the method will be hidden.

 

Here is an example of the same:

 

public class ClassA {

  public void show(){
    System.out.println("ClassA.show() called");
  }
}

public class ClassB extends ClassA{

  public void show(){
    System.out.println("ClassB.show() called");
  }
}

public class InheritanceExample {

  public static void main(String[] args) {
    
    ClassB obj = new ClassB();
    obj.show();
  }

}


 

In the above example, both the classes ClassA and ClassB define the method show(). The method has the same name and type signature. So, when the method is invoked using the child class instance, the subclass’s show() method will be called.

 

This will print :

 

ClassB.show() called

 

Method overriding occurs only when the names and type signatures of the two methods are identical. If they are not, the methods are overloaded, but not overridden.

 

Here is an example of the same:

 

public class ClassA {

  public void show(){
    System.out.println("ClassA.show() called");
  }
}

public class ClassB extends ClassA{

  public void show(String msg){
    System.out.println(msg + " ClassB.show() called");
  }
}

public class InheritanceExample {

  public static void main(String[] args) {
    
    ClassB obj = new ClassB();
    obj.show();
    obj.show("Hi");
  }

}

In the above example, the signature of show() method is different between ClassA and ClassB. The method in ClassA does not take any parameters, whereas the method in classB takes a String parameter.

 

So, the methods are overloaded and not overridden. Calling the method without any parameter will invoke the superclass method here and calling show() with a String parameter will invoke the subclass method.

 

Here is the output of the above program :

ClassA.show() called
Hi ClassB.show() called

 

Runtime polymorphism with overridden methods

 

Polymorphism is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the nature of the situation.

 

This is often expressed as “one interface, multiple methods”.

 

Java supports runtime polymorphism through overridden methods.

 

The superclass provides methods that the subclass can use directly. It also provides methods that the subclass should implement on its own. This allows the subclass flexibility to define own methods while following a consistent interface.

 

The method in which Java implements run-time polymorphism is known as Dynamic Method Dispatch.

 

Dynamic Method dispatch

 

This is a mechanism by which a call to an overridden method is resolved at run time, rather than at compile time.

 

A superclass reference variable can refer to a subclass object. Java uses this to resolve calls to overridden methods at run time.

 

If a superclass contains a method that is overridden by a subclass, then when different types of objects are referred through a superclass reference variable, different versions of the method are executed.

 

The determining factor is the type of the object that is being referred and not the type of the reference variable.

 

Here is an example that explains this :

 

public class ClassA {

  public void show(){
    System.out.println("ClassA.show() called");
  }
}

public class ClassB extends ClassA{

  public void show(){
    System.out.println("ClassB.show() called");
  }
}

public class InheritanceExample {

  public static void main(String[] args) {
    ClassA objA ;
    
    //objA refers to ClassA object
    objA = new ClassA();
    //call ClassA method show()
    objA.show();
    
    //objA refers to ClassB object
    objA = new ClassB();
    //call ClassB method show()
    objA.show();
  }

}


 

This program will print :
ClassA.show() called
ClassB.show() called

 

When the objA reference variable points to ClassA object, ClassA’s show() method is called.
Similarly, when objA points to ClassB object, ClassB’s show() method is called.

 

 

© 2015, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags