Polymorphism in java
Published: 2021-10-18 12:23:08

<b>Polymorphism</b> 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;
<b>1. Runtime polymorphism / Dynamic Binding / Dynamic dispatch method</b>: 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.
<b>Method overriding</b> 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.
<b>Example of Method overriding</b>
```java
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(); 
  } 
}

```
<b>Output</b>
```
Mammals do eat.
A person eats food like chips,meat,rice etc.
A cow eats grass.
```


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

}

```
<b>Output</b>
```
The calculated interest at Stanbic bank is 229500
The calculated interest at Equity bank is 213750.0
```

<b>Notes:</b> 
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.