Polymorphism in Java

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.

There are two types of polymorphism in Java

  • compiletime polymorphism
  • runtime polymorphism

Compile-time polymorphism

Compiletime polymorphism in java is that Polymorphism which is resolved during compiler time is known as compiler time polymorphism or static polymorphism

Compile-Time polymorphism is achieved through Method Overloading

Compiler time polymorphism example

class SimpleCalculator
{
    int add(int a, int b)
    {
         return a+b;
    }
    int  add(int a, int b, int c)  
    {
         return a+b+c;
    }
}
public class Demo
{
   public static void main(String args[])
   {
	   SimpleCalculator obj = new SimpleCalculator();
       System.out.println(obj.add(10, 20));
       System.out.println(obj.add(10, 20, 30));
   }
}

Output

30
60

Runtime Polymorphism in Java or Dynaimc method dispatch

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compiletime.

In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

what is Upcasting ?

If the reference variable of Parent class refers to the object of Child class, it is known as upcasting

Example

class A{}
class B extends A{}
 A a=new B();//upcasting

Example of Java Runtime Polymorphism

In this example, we are creating two classes Bike and Splendor. Splendor class extends Bike class and overrides its run() method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, the subclass method is invoked at runtime.

Since method invocation is determined by the JVM not compiler, it is known as runtime polymorphism.

class Animal{  
  void eat(){
System.out.println("Animals Eat");
}  
}  
class herbivores extends Animal{  
  void eat(){
System.out.println("Herbivores Eat Plants");
} 
  }
class omnivores extends Animal{  
  void eat(){
System.out.println("Omnivores Eat Plants and meat");
} 
  }
class carnivores extends Animal{  
  void eat(){
System.out.println("Carnivores Eat meat");
} 
  }
class main{
  public static void main(String args[]){ 
    Animal A = new Animal();
    Animal h = new herbivores(); //upcasting  
    Animal o = new omnivores(); //upcasting  
    Animal c = new carnivores(); //upcasting  
    A.eat();
    h.eat();
    o.eat();  
    c.eat();  
   
  }  
}  

output

Animals eat
Herbivores Eat Plants
Omnivores Eat Plants and meat
Carnivores eat meat

Java Runtime Polymorphism Example: Bank

Consider a scenario where Bank is a class that provides a method to get the rate of interest. However, the rate of interest may differ according to banks. For example, SBI, ICICI, and AXIS banks are providing 8.4%, 7.3%, and 9.7% rate of interest

class Bank{
 float getRateOfInterest(){return 0;}
}
 class SBI extends Bank{
float getRateOfInterest(){return 8.4f;}
}
 class ICICI extends Bank{
 float getRateOfInterest(){return 7.3f;}
 }
 class AXIS extends Bank{
 float getRateOfInterest(){return 9.7f;}
}
 class TestPolymorphism{
public static void main(String args[]){
Bank b;
 b=new SBI();
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
b=new ICICI();
System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
 b=new AXIS();
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
 }
}

Output

SBI Rate of Interest: 8.4
ICICI Rate of Interest: 7.3
AXIS Rate of Interest: 9.7

Leave a Comment