# Static Polymorphism Dynamic Polymorphism

## Static Polymorphism Dynamic Polymorphism

Summary: By the end of this tutorial "Static Polymorphism Dynamic Polymorphism", you will understand their differences. Explained with programs in simple words.

It is the continuation of previous program Difference: Static binding and Dynamic binding and knowledge of binding is very much required for better understanding this concept as binding leads to polymorphism or without binding there is no polymorphism.

###### A) Program on Static Polymorphism
```public class Area
{
public void calculate()		         // I
{
System.out.println("Nothing to calculate");
}
public void calculate(int x)		         // II
{
System.out.println("Circle Area: " + Math.PI * Math.pow(x, 2));
}
public void calculate(double x)		 // III
{
System.out.println("Circle Perimeter: " + 2 * Math.PI * x);
}
public void calculate(int x, int y)	         // IV
{
System.out.println("Rectangle Area: " +x*y);
}
public static void main(String args[])
{
Area a1 = new Area();
a1.calculate();		    // I
a1.calculate(10, 20);	    // IV
a1.calculate(10.5);		    // III
a1.calculate(10);		    // II
}
}
```

In the above example, calculate() method is called four times and each time it gives different outputs. This is called Polymorphism. In an OOPs language, the same method called different times gives different outputs is known as Polymorphism (poly=many, morphism=form). Otherway, many forms of the same method. For example, I say God is polymorphic. God is only one but has different forms like Lord Balaji, Goddess Laxmi, Goddess Saraswathi and infact, each one fulfils different wishes like Chilkur Balaji in Hyderabad helps in Visa stamping.

As this polymorphism is achieved through static binding, it is known as static polymorphism.

###### B) Dynamic Polymorphism

Observe the program given in the same "Static binding and Dynamic bindingg".

```class Lecturer
{
public void call()		// I
{
System.out.println("Hello Sir, Good morning");
}
}
class Raju extends Lecturer
{
public void call()		// II
{
System.out.println("Hello Raju, improve your programming skills");
}
}

{
public void call()		// III
{
}
}

class Jyostna extends Lecturer
{
public void call()		// IV
{
System.out.println("Hello Jyostna, improve your aptitude and reasoning skills");
}
}
public class DynamicBindingExample
{
public static void main(String args[])
{
Lecturer l1 = new Lecturer();  l1.call();                      // I
Raju r1 = new Raju();	   l1 = r1;		l1.call(); // II
Jyostna j1 = new Jyostna();	   l1 = j1;		l1.call(); // IV
}
}
```

Here also, the same method call() called different times gives different outputs of calling Raju, Prasad and Jyostna with different advices. This is known as polymorphism. As polymorphism is achieved through dynamic binding, it is known as dynamic polymorphism.

Principle involved in Dynamic binding

l1 = r1;
l1.call();

If a subclass object is assgined to super class object, the super class object will call subclass overridden method. Observe, when r1 is assigned to l1, super class l1 will call subclass r1 call() method.

Property Static Polymorphism Dynamic Polymorphism
Occurrence Occurs at compile-time Occurs at runtime
Achievement Achieved through static binding Achieved through dynamic binding
Inheritance Inheritance is not involved Inheritance is involved
Classes Happens in the same class Happens between two classes
Object assignment Object assignment is not required Required where a subclass object is assigned to super class object

Pass your suggestions for the betterment of this tutorial "Static Polymorphism Dynamic Polymorphism".

View All for Java Differences on 90 Topics

### 5 thoughts on “Static Polymorphism Dynamic Polymorphism”

1. “Required where a subclass object is assigned to super class object”. This statement is technically wrong, it should be “Required where a subclass object is assigned to super class reference”.

It is super class’ reference that is referring to sub class object and not super class object.