DEV Community

Ravi Yasas
Ravi Yasas

Posted on

Runtime Polymorphism Compile-time polymorphism vs Run-time polymorphism

In this post, you will understand,

  • What is run-time polymorphism?
  • What is compile-time polymorphism?
  • What does it happen in the compile-time & run-time?

Run-time polymorphism

You may look at the following classes.

//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();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, I have created a parent class, child class, and the main class. Then I have created a Child object by Parent reference. Now let's check what happens in the compile-time and run-time.

Compile-time

  • In compile time it takes care of 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 a success.

Run time

  • At the run time it takes care of the Child reference and it creates the new child object.
  • Then it checks is there any eat() method in Child class.
  • There is an eat() method in Child also.
  • Then this method will be called at the run time.
  • It means object creation is happening 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)

Compile-time polymorphism

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this case, a single class has the 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 the compilation is OK.

Run time

  • At the run time it creates the same Animal type object.
  • Then it checks the same thing that it did at 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).

Final words !

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

More info: www.javafoundation.xyz

Top comments (0)