DEV Community

Dallington Asingwire
Dallington Asingwire

Posted on • Updated on

Polymorphism in java

Polymorphism is the ability of an object to take many forms. It allows a single action/task to be performed in many ways.
Polymorphism comes from 2 Greek words i.e poly and morphs. Poly means many while morphs means forms.There are 2 types of polymorphism;
1. Runtime polymorphism / Dynamic Binding / Dynamic dispatch method: With this, a call to a method is resolved dynamically(at runtime rather than at compile-time). Dynamic binding is achieved using method overriding in java.
Method overriding is when a subclass(child) has a method with the same method signature as that of a superclass (parent) but with a different method definition.
Example of Method overriding

public class Mammal { 
  public void eat(){ 
    System.out.println(β€œMammals do eat.”); 
  } 
}

public class Person extends Mammal { 
public void eat(){ 
   System.out.println('A person eats food 
      like chips,meat,rice etc.'); 
  } 
}

public class Cow extends Mammal { 
public void eat(){ 
    System.out.println('A cow eats grass.'); 
   } 
}

public class Main{
   public static void main(String[] args){ 
    Mammal m = new Mammal();
    Person p = new Person();
    Cow c = new Cow();
    m.eat(); 
    p.eat(); 
    c.eat(); 
  } 
}

Enter fullscreen mode Exit fullscreen mode

Output

Mammals do eat.
A person eats food like chips,meat,rice etc.
A cow eats grass.
Enter fullscreen mode Exit fullscreen mode

2. Compile time or static polymorphism: With compile-time polymorphism, a call to a method is resolved at compile time. This can be achieved through method overloading in java.
Method overloading is when a class has multiple methods with the same name but different method signatures and method definition.
Real life example: Banks can charge or have different rates of interest depending on their financial policies.
Code Example

public class Bank { 
   public double getInterest(double principal, int time){ 
     return 4*principal*(time/100);
   } 
}

public class StanbicBank extends Bank { 
   public double getInterest(double principal, double time){ 
      return 6*principal*(time/100);
   } 
}

public class EquityBank extends Bank { 
    public float getInterest(int principal, float time){ 
     return  5*principal*(time/100);
    } 
}

public class Main{
public static void main(String[] args) { 
StanbicBank s = new StanbicBank(); 
EquityBank e = new EquityBank(); 

  System.out.print("The calculated interest "
           + "at Stanbic bank is 
                   "+Math.round(s.getInterest(450000, 8.5)));

   System.out.print("\nThe calculated interest at"
        + " Equity bank is "+e.getInterest(450000, 9.5f));
  } 

}

Enter fullscreen mode Exit fullscreen mode

Output

The calculated interest at Stanbic bank is 229500
The calculated interest at Equity bank is 213750.0
Enter fullscreen mode Exit fullscreen mode

Notes:

  1. You note that with method overloading, the signature for the method getInterest() changes for each bank while with method overriding, the method eat() does not change the signature for 2 classes (Person and Cow) but rather changes the method definition.
  2. Method signature refers to the method name, parameters/input to the method and their datatypes and then method return type.

This is how polymorphism is achieved in java. You can think of many real life examples and make use of this concept in your programming practices or projects. Thank you for taking a minute to read through this post.

Top comments (0)