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

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


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   }}


  • 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


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 differenttypes of documents.

  • We can have ‘Document’ as the base class and different document typeclasses 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 thesemethods in its own way based on the actual contents of the documents.

  • When different types of documents need to beserialized/de-serialized, the document objects will be referred bythe ‘ 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 noarguments and one taking a prefix string to be displayed along withthe employee data.

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

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