What is the difference between dynamic and static polymorphism in Java?

247,677

Solution 1

Polymorphism

1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)

2. Dynamic binding/Run-Time binding/Late binding/Method overriding.(in different classes)

overloading example:

class Calculation {  
  void sum(int a,int b){System.out.println(a+b);}  
  void sum(int a,int b,int c){System.out.println(a+b+c);}  

  public static void main(String args[]) {  
    Calculation obj=new Calculation();  
    obj.sum(10,10,10);  // 30
    obj.sum(20,20);     //40 
  }  
}  

overriding example:

class Animal {    
   public void move(){
      System.out.println("Animals can move");
   }
}

class Dog extends Animal {

   public void move() {
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog {

   public static void main(String args[]) {
      Animal a = new Animal(); // Animal reference and object
      Animal b = new Dog(); // Animal reference but Dog object

      a.move();//output: Animals can move

      b.move();//output:Dogs can walk and run
   }
}

Solution 2

  • Method overloading would be an example of static polymorphism

  • whereas overriding would be an example of dynamic polymorphism.

    Because, in case of overloading, at compile time the compiler knows which method to link to the call. However, it is determined at runtime for dynamic polymorphism

Solution 3

Dynamic (run time) polymorphism is the polymorphism existed at run-time. Here, Java compiler does not understand which method is called at compilation time. Only JVM decides which method is called at run-time. Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.

For example,

  • Consider an application that serializes and de-serializes different types of documents.

  • We can have ‘Document’ as the base class and different document type classes deriving from it. E.g. XMLDocument , WordDocument , etc.

  • Document class will define ‘ Serialize() ’ and ‘ De-serialize() ’ methods as virtual and each derived class will implement these methods in its own way based on the actual contents of the documents.

  • When different types of documents need to be serialized/de-serialized, the document objects will be referred by the ‘ Document’ class reference (or pointer) and when the ‘ Serialize() ’ or ‘ De-serialize() ’ method are called on it, appropriate versions of the virtual methods are called.

Static (compile time) polymorphism is the polymorphism exhibited at compile time. Here, Java compiler knows which method is called. Method overloading and method overriding using static methods; method overriding using private or final methods are examples for static polymorphism

For example,

  • An employee object may have two print() methods one taking no arguments and one taking a prefix string to be displayed along with the employee data.

  • Given these interfaces, when the print() method is called without any arguments, the compiler, looking at the function arguments knows which function is meant to be called and it generates the object code accordingly.

For more details please read "What is Polymorphism" (Google it).

Solution 4

Binding refers to the link between method call and method definition.

This picture clearly shows what is binding.

binding

In this picture, “a1.methodOne()” call is binding to corresponding methodOne() definition and “a1.methodTwo()” call is binding to corresponding methodTwo() definition.

For every method call there should be proper method definition. This is a rule in java. If compiler does not see the proper method definition for every method call, it throws error.

Now, come to static binding and dynamic binding in java.

Static Binding In Java :

Static binding is a binding which happens during compilation. It is also called early binding because binding happens before a program actually runs

.

Static binding can be demonstrated like in the below picture.

enter image description here

In this picture, ‘a1’ is a reference variable of type Class A pointing to object of class A. ‘a2’ is also reference variable of type class A but pointing to object of Class B.

During compilation, while binding, compiler does not check the type of object to which a particular reference variable is pointing. It just checks the type of reference variable through which a method is called and checks whether there exist a method definition for it in that type.

For example, for “a1.method()” method call in the above picture, compiler checks whether there exist method definition for method() in Class A. Because ‘a1′ is Class A type. Similarly, for “a2.method()” method call, it checks whether there exist method definition for method() in Class A. Because ‘a2′ is also Class A type. It does not check to which object, ‘a1’ and ‘a2’ are pointing. This type of binding is called static binding.

Dynamic Binding In Java :

Dynamic binding is a binding which happens during run time. It is also called late binding because binding happens when program actually is running.

During run time actual objects are used for binding. For example, for “a1.method()” call in the above picture, method() of actual object to which ‘a1’ is pointing will be called. For “a2.method()” call, method() of actual object to which ‘a2’ is pointing will be called. This type of binding is called dynamic binding.

The dynamic binding of above example can be demonstrated like below.

enter image description here

Reference static-binding-and-dynamic-binding-in-java

Solution 5

method overloading is an example of compile time/static polymorphism because method binding between method call and method definition happens at compile time and it depends on the reference of the class (reference created at compile time and goes to stack).

method overriding is an example of run time/dynamic polymorphism because method binding between method call and method definition happens at run time and it depends on the object of the class (object created at runtime and goes to the heap).

Share:
247,677
Prabhakar Manthena
Author by

Prabhakar Manthena

Ninja Software Engineer at VOZIQ.

Updated on April 10, 2020

Comments

  • Prabhakar Manthena
    Prabhakar Manthena about 4 years

    Can anyone provide a simple example that explains the difference between Dynamic and Static polymorphism in Java?

  • Meet
    Meet over 10 years
    *(object create at run time and goes to heap), it should run time
  • lu5er
    lu5er almost 7 years
    I'm new to Java, so just curios what is the underlying concept between Animal reference but Dog object, why can't we use Dog reference and dog object?
  • KhAn SaAb
    KhAn SaAb almost 7 years
    In the above example I tried to show the concept of polymorphism. we can create the reference and object of same class but we cant achieve method overriding. please go through with below post : stackoverflow.com/questions/12159601/…
  • AnBisw
    AnBisw over 6 years
    better than before.
  • John Red
    John Red about 6 years
    This answer full of errors: (1) Method overloading is not dynamic polymorphism. It is static polymorphism. (2) Static methods are never overridden, they are hidden/shadowed. (3) Private methods are not "overridden". They are never inherited in the first place.
  • prime
    prime about 6 years
    Generally overloading won't be considered as polymorphism. can you please elaborate on this point.
  • Ravindra babu
    Ravindra babu about 6 years
    Dynamic binding and overriding is striking point for polymorphism
  • Gaali Prabhakar
    Gaali Prabhakar almost 6 years
    method overloading is Compile time polymorphism. is same way. is constructor overloading also Compile time polymorphism ?