Tuesday, November 21, 2017

Compile-time polymorphism vs Run-time polymorphism



This is very important interview question. Simply Compile-time polymorphism is known as overriding and Run-time polymorphism is known as overloading. I think you may know about the differences between both of overloading and overriding. (OOP concepts)


But question is this, If someone asked why it is called compile-time polymorphism and run-time polymorphism ? Lets try to find out the answers by examples.

Overriding is called run-time polymorphism. WHY ???


Look at the following example.

//Parent class
class Parent {
    public void eat(){
        System.out.println("Parent - eat()");
    }
}

//Child class
class Child extends Parent {

    @Override
    public void eat() {
        System.out.println("Child - eat()");
    }
}

public class OOPDemo {

    public static void main(String args[]){
        Parent obj = new Child();
        obj.eat();
    }
}



In this example I've created a parent class, child class and the main class. Then I've created a Child object by Parent reference. 

Compile time
  • In compile time it takes care about the Parent reference. Because objects are created in the run time
  • First it checks is there any eat() method in Parent class. 
  • In this example Parent class has eat() method. 
  • So it doesn't give any exception in the compile time and compilation will be success.
Run time
  • In run time it takes care about the Child reference and it creates new Child object.
  • Then it checks is there any eat() method in Child class. 
  • There is a eat() methods in Child also. Then this method will be called in the run time. 
  • It means object creation is happen at the run time by JVM and Run time is responsible to call which eat() method to call.
  • That is why it is called run-time polymorphism (Dynamic polymorphism)

Overloading is called compile-time polymorphism. WHY ???

Look at the following example.

class Animal{

    public void eat(){
        System.out.println("Eat method");
    }

    public void eat(int quantity){
        System.out.println("Eat method with quantity");
    }
}


public class ReferenceDemo {
    public static void main(String args[]){
        Animal animal = new Animal();
        animal.eat();
    }
}



In this case single class has same methods with two different parameters. As you know it is called method overloading. 

Compile time
  • In compile time it checks animal.eat() method in Animal class. 
  • No arg eat() method is there in the animal class.
  • According to the parameter list it will call which method should be called.
  • Then compilation is OK.

Run time
  • In run time it creates same Animal type object.
  • Then it checks the same thing what is did in the compile time.
  • It means compile time is enough to check which method should be called.
  • That is why it is called Compile time polymorphism ( Static polymorphism).


Finally

Overriding    = run-time polymorphism         = dynamic polymorphism
Overloading  = compile-time polymorphism  = static polymorphism