Difference between compile-time polymorphism and runtime polymorphism


Polymorphism is one of the most important OOPs concepts. Its is a concept by which we can perform single task in multiple ways. There are two types of polymorphism one is Compile-time polymorphism and another is run-time polymorphism.

Method overloading is the example of compile time polymorphism and  method overriding is the example of run-time polymorphism.

Sr. No.KeyCompile-time polymorphismRuntime polymorphism
1BasicCompile time polymorphism means binding is occuring at compile timeR un time polymorphism where at run time we came to know which method is going to invoke
2Static/DynamicBindingIt can be achieved through static bindingIt can be achieved through dynamic binding
4.InheritanceInheritance is not involvedInheritance is involved
5ExampleMethod overloading is  an example of compile time polymorphismMethod overriding is an example of runtime polymorphism

Example of Compile-time Polymorphism

public class Main {
   public static void main(String args[]) {
      CompileTimePloymorphismExample obj = new CompileTimePloymorphismExample();
      obj.display();
      obj.display("Polymorphism");
   }
}
class CompileTimePloymorphismExample {
   void display() {
      System.out.println("In Display without parameter");
   }
   void display(String value) {
      System.out.println("In Display with parameter" + value);
   }
}

Example of Runtime Polymorphism

public class Main {
   public static void main(String args[]) {
      RunTimePolymorphismParentClassExample obj = new RunTimePolymorphismSubClassExample();
      obj.display();
   }
}

class RunTimePolymorphismParentClassExample {
   public void display() {
      System.out.println("Overridden Method");
   }
}

public class RunTimePolymorphismSubClassExample extends RunTimePolymorphismParentExample {

   public void display() {
      System.out.println("Overriding Method");
   }
}

Updated on: 12-Sep-2023

29K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements