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.

Method overloading leads to static binding and static binding leads to static polymorphism.

Method overloading –> Static Binding –> 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");
  }
}

class Prasad extends Lecturer
{
  public void call()		// III
  {
    System.out.println("Hello Prasad, improve your English language");
  }
}

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
    Prasad p1 = new Prasad();	   l1 = p1;		l1.call(); // III
    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
Required methods Method overloading should exist Method overriding should exist
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.

  2. Muhammad Jamil

    It’s so informative website
    my question is ,when superclass object refer to subclass object and super class object belong to superclass and can access all super class methods as well as fields i mean it’s purely superclass object .why superclass object can not call subclass methods that are not overriden as well as fields . i m thanks

  3. sir,
    why it is called dynamic binding? superclass is pointing subclass that is known at the compile time.

Leave a Comment

Your email address will not be published.