Runtime Polymorphism in Java

This article explains one of the most important concepts of Object Oriented Programming, Polymorphism, with several sets of examples along with definitions and related diagrams.
 

Runtime Polymorphism

 
A process in which a call to an overridden method is resolved at runtime rather than compile time is known as Runtime Polymorphism.
 
It is also called Dynamic method dispatch.
 
The idea behind this concept is that the overridden method is called by the reference variable of the superclass, in other words the parent class. The method to be called is determined on the basis of the object being referred to by the reference variable.
 

Upcasting

 
When the reference variable of the parent class is called by the object of the child class, it is known as upcasting.
 
Upcasting 
 
  1. class Student {}  
  2. class Monitor extends Student{}  
  3. Student s=new Monitor(); // upcasting  

Example of runtime Polymorphism

 
In this example, we created the two classes Car and Swift. Car is the parent, in other words the superclass and Swift is the child, in other words the subclass. The Swift class extends the Car class and overrides its methods.
 
We are calling the Speed method by the reference variable of the parent class. Since it refers to the subclass object of the child class and the subclass method overrides the parent class method, the subclass method is invoked at runtime.
 
Since the method is invoked by the JVM, not the compiler, it is called runtime polymorphism.
  1. class Car {  
  2.     void speed() {  
  3.         System.out.println("Speed should be more than a bike");  
  4.     }  
  5. }  
  6. class Swift extends Car {  
  7.     void speed() {  
  8.         System.out.println("Speed is 200km/hr.");  
  9.     }  
  10.     public static void main(String args[])  
  11.     {  
  12.         Car c = new Swift(); // upcasting  
  13.         c.speed();  
  14.     }  
  15. }  
Output
 
Speed is 200km/hr.
 
In the preceding example “c” is the reference variable. The subclass, in other words the child class method, is invoked at runtime.
 
Example | Runtime Polymorphism
 
Consider the scenario that the bike is the class that provides methods to get the top speed limit.
 
Companies provide varying top speeds. For example Honda-150km/hr, TVS-180km/hr, Yamaha-200km/hr.
 
Runtime Polymorphism 
 
Example | Hierarchical inheritance
 
This example shows the functioning of the hierarchical inheritance.
  1. class Bike  
  2. {  
  3.     int topSpeedLimit()  
  4.     {  
  5.         return 0;  
  6.     }  
  7. }  
  8. Class Honda extends Bike  
  9. {  
  10.     int topSpeedLimit()  
  11.     {  
  12.         return 150;  
  13.     }  
  14. }  
  15. class Yamaha extends Bike  
  16. {  
  17.     int topSpeedLimit()  
  18.     {  
  19.         return 180;  
  20.     }  
  21. }  
  22. Class Tvs extends Bike  
  23. {  
  24.     int topSpeedLimit()  
  25.     {  
  26.         return 200;  
  27.     }  
  28. }  
  29. class Test  
  30. {  
  31.     public static void main(String args[])  
  32.     {  
  33.         Bike b1 = new Honda();  
  34.         Bike b2 = new Yamaha();  
  35.         Bike b3 = new Tvs();  
  36.         System.out.println("Top speed of Honda:" + b1.topSpeedLimit());  
  37.         System.out.println("Top speed of Yamaha:" + b2.topSpeedLimit());  
  38.         System.out.println("Top speed of Tvs:" + b3.topSpeedLimit());  
  39.     }  
  40. }  
Output
 
Top speed of Honda:150
Top speed of Yamaha:180
Top speed of Tvs:200
 
Runtime Polymorphism can be categorized in these two parts:
 
Polymorphism in Java 
 

Runtime Polymorphism with Data member

 
The method is overridden but not the data members, so runtime polymorphism cannot be done with data members.
 
In the example given below, we created a Student class and Monitor class. Both the classes have data members, in other words name and roll number. We are accessing the data members by the reference variable of the parent class that refers to the subclass object.
 
Since we are accessing the data members of the subclass that are not overridden, the data members of the parent class will always be invoked.
  1. Class Student  
  2. {  
  3.     String name="Arunesh";  
  4. }  
  5. Class Monitor extends Student  
  6. {  
  7.    String name="Amrita";  
  8.    public static void main(String args[])  
  9.    {  
  10.       Student s=new Monitor();  
  11.       System.out.println(s.name);  
  12.    }  
  13. }   
Output: Arunesh
 

Runtime Polymorphism with multilevel inheritance

 
Let's see a simple example.
  1. class Electronics {  
  2.     void  
  3.     function() {  
  4.         System.out.println("Made for various purpose");  
  5.     }  
  6. }  
  7. class Phone extends Electronics {  
  8.     void  
  9.     function() {  
  10.         System.out.println("Display,Voice");  
  11.     }  
  12. }  
  13. class Computer extends Phone {  
  14.     void  
  15.     function() {  
  16.         System.out.println("Calculation,Internetworking");  
  17.     }  
  18.     public static void main(String args[])  
  19.     {  
  20.         Electronics e1 = new Electronics();  
  21.         Electronics e2 = new Phone();  
  22.         Electronics e3 = new Computer();  
  23.         e1.function();  
  24.         e2.function();  
  25.         e3.function();  
  26.     }  
  27. }  
Output
 
Made for various purposes.
Display,Voice
Calculation,Internetworking
 
Analyze this example and try to figure out what happens here.
  1. class Electronics  
  2. {  
  3.     void  
  4.     function()  
  5.     {  
  6.         System.out.println("Made for various purpose");  
  7.     }  
  8. }  
  9. class Phone extends Electronics  
  10. {  
  11.     void  
  12.     function()  
  13.     {  
  14.         System.out.println("Display,Voice");  
  15.     }  
  16. }  
  17. class Computer extends Phone  
  18. {  
  19.     public static void main(String args[])  
  20.     {  
  21.         Electronics e = new Computer();  
  22.         e.function();  
  23.     }  
  24. }  
Output: Display,Voice
 
Here the Computer class does not override the function() method, so the function() method of the phone class is invoked.


Similar Articles