Polymorphism

Polymorphism is the concept where an object behaves differently in different situations

Since the object takes multiple forms, it is called Polymorphism.

Types of Polymorphism


polymorphism


Static Polymorphism (compile time polymorphism/ Method overloading):

Method Overloading
In a class,if we have multiple methods with same name but different in parameters then it is called Method overloading.


Whenever we need to perform similar operation but by using different number of parameters or different type of parameters then using same name for a method will increase the readability of the code.

Example

If we want to perform the addition of numbers using 2 parameters and 3 parameters then we can write 2 separate methods with the same name but with different number of parameters.

1
2
3
4
5
6
7
8
Class Addition{
Public int add(int x,int y){
Return x+y;
}
Public int add(int x,int y,int z){
Return x+y+z;
}
}
Class Addition{
Public int add(int x,int y){
Return x+y;
}
Public int add(int x,int y,int z){
Return x+y+z;
}
}


Now whenever we need to call these methods , we can pass appropriate number of parameters and compiler will be able to indentify the exact matching method based on the parameter.

1
2
3
4
5
6
7
Class AdditionTest{
public static void main(String[] args){ 
Addition addObj = new Addition();
System.out.println(addObj.add(10,20));  
System.out.println(addObj.add(10,20,30));  
}
}
Class AdditionTest{
public static void main(String[] args){ 
Addition addObj = new Addition();
System.out.println(addObj.add(10,20));  
System.out.println(addObj.add(10,20,30));  
}
}


Since the decision of identifying the right method is taken at compile time itself, its called Compile time polymorphism.

Dynamic Polymorphism (run time polymorphism/ Method Overriding)


Before understanding Dynamic polymorphism, we need to have clear understanding of method overriding, because Dynamic polymorphism has to be achieved only by using method overriding.

Method overriding:
If the subclass or a child class has the same method (both the methods have same name and signature) as defined in its parent or super class then its called Method overriding. Both methods should have same name and signature but differ in their implementation.

Method overriding helps us to achieve Runtime polymorphism.

Method overriding Rules


1) There must be Inheritance or parent child relationship between the classes

2) Both the methods (in parent and child class) should have the same name

3) Both the methods (in parent and child class) should have the same signature

Example:


Vehicle.java

1
2
3
4
5
6
7
8
public class Vehicle {  
    public void start(){
     System.out.println("Vehicle start logic");  
    }
    public void stop(){  
        System.out.println("Vehicle stop logic");  
    }  
}  
public class Vehicle {  
    public void start(){
	 System.out.println("Vehicle start logic");  
	}
    public void stop(){  
        System.out.println("Vehicle stop logic");  
    }  
}  


TwoWheeler.java

1
2
3
4
5
6
7
8
9
10
11
class TwoWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Two wheeler start logic");       
  }    
 
 @Override  
public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
}  
class TwoWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Two wheeler start logic");       
  }    

 @Override  
public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
}  


FourWheeler.java

1
2
3
4
5
6
7
8
9
10
11
class FourWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Four Wheeler start logic");  
    }  
    
      @Override  
    public void stop() {  
        System.out.println("Four Wheeler stop logic");  
    } 
}
class FourWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Four Wheeler start logic");  
    }  
	
	  @Override  
    public void stop() {  
        System.out.println("Four Wheeler stop logic");  
    } 
}


VehiclManager.java

1
2
3
4
5
6
7
8
9
10
public class VehicleManager{  
    public static void main(String[] args) {  
        Vehicle twoWheeler = new TwoWheeler();  
        Vehicle  fourWheeler = new FourWheeler();  
        twoWheeler.start();  
        twoWheeler.stop();  
        fourWheeler.start();  
        fourWheeler.stop();  
    }  
}  
public class VehicleManager{  
    public static void main(String[] args) {  
        Vehicle twoWheeler = new TwoWheeler();  
        Vehicle  fourWheeler = new FourWheeler();  
        twoWheeler.start();  
        twoWheeler.stop();  
        fourWheeler.start();  
        fourWheeler.stop();  
    }  
}  




Why do we need method overriding?


Whenever we need to provide different implementation in the subclass for a method which is already defined in the parent class, we need to go for method overriding.

As shown in the above example, start and stop methods definition is different for Two Wheelers and Four Wheelers than what is provided in the Parent class Vehicle.

So we need to override those methods with new definition.

Runtime polymorphism is the process of resolving a call to an overridden method at runtime rather than at compile-time.

Since overridden method will have same name and same signature, Its not possible to detect the exact method call during compile time.

At run time, it checks the reference variable pointing to which object and accordingly the exact method call will be resolved.

Since this method resolution happens at run time, its called Runtime polymorphism.

In the above Example, If we look at below line

1
Vehicle twoWheeler = new TwoWheeler();  
Vehicle twoWheeler = new TwoWheeler();  


We have created the reference variable of type Vehicle and its named as “twoWheeler”.

This reference variable is pointing to an object of TwoWheeler class.

Hence below lines will make a call to the start() and stop() methods available inside “TwoWheeler” class.

1
2
        twoWheeler.start();  
        twoWheeler.stop();  
        twoWheeler.start();  
        twoWheeler.stop();  


Similary, If we look at below line

1
 Vehicle  fourWheeler = new FourWheeler();  
 Vehicle  fourWheeler = new FourWheeler();  


We have created the reference variable of type Vehicle and its named as “fourWheeler”.

This reference variable is pointing to an object of FourWheeler class.

Hence below lines will make a call to the start() and stop() methods available inside “FourWheeler” class.

1
2
        fourWheeler.start();  
        fourWheeler.stop();  
        fourWheeler.start();  
        fourWheeler.stop();  


We can observe that variable of same “Vehicle” type is calling methods of TwoWheeler class and FourWheeler class based on what object is assigned to it.

This process is called polymorphism as variable of same type is calling methods from different classes at different time.

Since these methods call can be determined only at the runtime based on what object is pointing to by the reference variable, its called Runtime polymorphism.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on