Polymorphism in Java

What is Polymorphism in Java? 

 
Polymorphism refers to "having many forms" or more than one form. In OOPs, it is the ability to create a variable, function or object which has many form. In the Java Programming Language, two types of Polymorphism are there:
 

1. Compile Time Polymorphism

 
Also known as Early Binding or Static Binding. In this case, the compiler identifies which polymorphic form is to be executed at compile time. Method overloading and Operator overloading are good examples of Compile Time Polymorphism. The following written program demonstrates Compile Time Polymorphism.
 

Method Overloading

 
When a class contains methods of the same name but of a different number of parameters, is called Method Overloading.
 
Code
  1. public class MethodOverloading {  
  2.     public static void main(String[] args) {  
  3.         MyClass obj = new MyClass();  
  4.         int sum = obj.Add(45);  
  5.         System.out.println(sum);  
  6.         sum = obj.Add(457);  
  7.         System.out.println(sum);  
  8.         sum = obj.Add(4579);  
  9.         System.out.println(sum);  
  10.     }  
  11. }  
  12. class MyClass {  
  13.     public int Add(int a, int b) {  
  14.         return a + b;  
  15.     }  
  16.     public int Add(int a, int b, int c) {  
  17.         return a + b + c;  
  18.     }    
  19.     public int Add(int a, int b, int c, int d) {  
  20.         return a + b + c + d;  
  21.     }  
  22. }  
Output
 
method-overloadinng 
 

2. Runtime Polymorphism

 
Also known as Late Binding or Virtual Method Invocation or Dynamic Binding. In Runtime Polymorphism, the compiler identifies which polymorphic form is to be executed at runtime but not in compile-time, is called Runtime Polymorphism. Method overriding is a well-known example of Runtime Polymorphism.
 

Method Overriding


Method Overriding takes place if Inheritance exists and a superclass and subclass have the method of the same signature. Signature refers to access specifier, return type, name, parameters(number, type & order) & throws clause of the method. Then, the method of the subclass overrides the method of the superclass. As demonstrated in the following written program.
 
Code
  1. public class MethodOverriding {  
  2.     public static void main(String[] args) {  
  3.         ClassB obj = new ClassB();  
  4.         obj.Display(1015);  
  5.     }  
  6. }  
  7. class ClassA {  
  8.     public void Display(int a, int b) {  
  9.         a += 5;  
  10.         b += 7;  
  11.         System.out.println("Sum is " + (a + b));  
  12.     }  
  13. }  
  14. class ClassB extends ClassA {    
  15.     public void Display(int a, int b) {  
  16.         System.out.println("Sum is " + (a + b));  
  17.     }  
  18. }  
Output
  
method-overriding
 
In this case, the method of ClassB overrides the method of ClassA. The method of ClassA is said to be Overridden. To call the overridden method, you need to use the super keyword as demonstrated in the following program.

Using "super" keyword to call the constructor of the superclass

 
Code
  1. public class SuperKeyword {  
  2.     public static void main(String[] args) {  
  3.         ClassC obj = new ClassC();  
  4.         obj.show();  
  5.     }  
  6. }  
  7. class ClassA {  
  8.     static int a;  
  9.     static int b;  
  10.     ClassA() {  
  11.         a = 7;  
  12.         b = 8;  
  13.     }  
  14. }  
  15. class ClassB extends ClassA {  
  16.     ClassB() {  
  17.         super();  
  18.     }  
  19. }  
  20. class ClassC extends ClassB {  
  21.     ClassC() {  
  22.         super();  
  23.     }  
  24.     public void show() {  
  25.         System.out.println("Sum is " + (a + b));  
  26.     }  
  27. }  
 
Output
 
super-keyword 
 
Note: Call to super must be first statement in constructor.

Hidden Method


Consider the case of method overriding, the method of the superclass is the overridden method. In the case of Method hiding, the concept is the same but methods in both classes are static. Now, the method of the subclass hides the method of the superclass. So the method of the superclass is called a Hidden Method. To call the hidden method, you need to provide the 'class reference', instead of using the super keyword. For example:

Code 
  1. public class MethodOverriding {  
  2.     public static void main(String[] args) {  
  3.         ClassB3 obj = new ClassB3();  
  4.         obj.Display(1015);  
  5.     }  
  6. }  
  7. class ClassA3 {  
  8.     public void Display(int a, int b) {  
  9.         a += 5;  
  10.         b += 7;  
  11.         System.out.println("Sum is " + (a + b));  
  12.     }  
  13. }  
  14. class ClassB3 extends ClassA3 {  
  15.     public void Display(int a, int b) {  
  16.         System.out.println("Sum is " + (a + b));  
  17.         super.Display(a, b);  
  18.     }  
  19. }  
Output
 
method-overriding-hidden-method 
 

Summary 

 
 In this article, we learned about Polymorphism and its concepts Method Overloading and Method Overriding ad how to use polymorphism in Java programs.