«Back to Home

Core Java

Topics

Polymorphism In Java

Polymorphism

Polymorphism means many forms. Polymorphism is the capability of an object to take on many forms. In Polymorphism, we can do a single task by different ways.
 
Java mainly use polymorphism in OOPS, which occurs when a parent class reference is used to refer a child class object.
 
In Java, there are two types of polymorphism, which are,
  • Compile time polymorphism.

  • Runtime polymorphism.
We can do polymorphism by method overloading and method overriding.
 
Compile time polymorphism

When we overload the static method in Java, it is called as a compile time polymorphism. In other words, we can say, if a class has more than one method with same name but different arguments.
 
Let’s see an example of the compile time polymorphism, given below.
 
Code
  1. public class Simplification {  
  2.     void add(int x, int y) {  
  3.         System.out.println(x + y);  
  4.     }  
  5.     void add(int x, int y, int z) {  
  6.         System.out.println(x + y + z);  
  7.     }  
  8.     public static void main(String args[]) {  
  9.         Simplification obj = new Simplification();  
  10.         obj.add(102030);  
  11.         obj.add(3020);  
  12.     }  
  13. }  
51
 
Output

52

In the example, shown above, we create add () method and overload that method with the different argument. It is an example of the compile time polymorphism.
 
Runtime Polymorphism

In Java, runtime polymorphism is a process in which we call an overridden method, which is resolved at the runtime. It is also called as a dynamic method dispatch.
 
In runtime polymorphism, an overridden method is called through the reference variable of a parent class.
 
Let’s see an example of runtime polymorphism, given below.
 
Code
  1. class Employee {  
  2.     void work() {  
  3.         System.out.println("working");  
  4.     }  
  5. }  
  6. public class Programmer extends Employee {  
  7.     void work() {  
  8.         System.out.println("working hard");  
  9.     }  
  10.     public static void main(String args[]) {  
  11.         Employee e = new Programmer(); //upcasting    
  12.         e.work();  
  13.     }  
  14. }  
53

Output

54
 
In the example, shown above, first we create two classes Employee and programmer. Programmer class inherits an employee class and overrides the work () method of the parent class. We call the work method by the reference variable of the parent class and it refers to the child class object. Hence, the child class method is invoked at the runtime by JVM.
 
What is Upcasting in Java?

In Java, it means that if the reference variable of the parent class refers to the child class object, it is called as an upcasting.
 
For example
  1. Employee e = new Programmer (); //upcasting   
Runtime polymorphism with Variable
 
In Java, runtime polymorphism can’t be achieved by variables because the method is overridden and not the variables
 
Let’s see an example, given below.
 
Code
  1. class Employee {  
  2.     int EmpId = 115;  
  3.     void work() {  
  4.         System.out.println("working");  
  5.     }  
  6. }  
  7. public class Programmer extends Employee {  
  8.     int EmpId = 303;  
  9.     void work() {  
  10.         System.out.println("working hard");  
  11.     }  
  12.     public static void main(String args[]) {  
  13.         Employee e = new Programmer(); //upcasting    
  14.         System.out.println("EmpId : " + e.EmpId);  
  15.         e.work();  
  16.     }  
  17. }  
55

Output

56

In the example, shown above, we create two classes’ employee and programmer and both classes have a same variable EmpId. Now, we access the variable by reference variable of the parent class, which refers to the child class object. In the example, mentioned above, we already read those variables, which are not overridden but only methods are overridden. Therefore, it will access the variable of the parent class for all time.
 
Runtime polymorphism with multilevel inheritance

Let’s see an example, given below.
 
Code
  1. class Employee {  
  2.     void work() {  
  3.         System.out.println("working");  
  4.     }  
  5. }  
  6. class Manager extends Employee {  
  7.     void work() {  
  8.         System.out.println("working hard");  
  9.     }  
  10. }  
  11. public class Developer extends Manager {  
  12.     void work() {  
  13.         System.out.println("working very hard");  
  14.     }  
  15.     public static void main(String args[]) {  
  16.         Employee e1, e2, e3;  
  17.         e1 = new Employee();  
  18.         e2 = new Manager();  
  19.         e3 = new Developer();  
  20.         e1.work();  
  21.         e2.work();  
  22.         e3.work();  
  23.     }  
  24. }  
57

Output

58

In the example, mentioned above, we create three classes and all have a same method work(). These classes represent the multilevel inheritance with the runtime polymorphism.
 
If the developer class is not overriding the work() method, time work() method of manager class is invoked.
 
For example.
 
Code
  1. class Employee {  
  2.     void work() {  
  3.         System.out.println("working");  
  4.     }  
  5. }  
  6. class Manager extends Employee {  
  7.     void work() {  
  8.         System.out.println("working hard");  
  9.     }  
  10. }  
  11. public class Developer extends Manager {  
  12.     public static void main(String args[]) {  
  13.         Employee e1;  
  14.         e1 = new Developer();  
  15.         e1.work();  
  16.     }  
  17. }  
59
 
Output

60

In the example, mentioned above, developer class is not overriding the work() method. Hence, work() method of manager class is invoked.
 
Summary

Thus, we learnt that in polymorphism, we can do a single task by different ways and also learnt how we can use it in Java.