Difference between Compile-time and Run-time Polymorphism in Java

Last Updated : 2 Jul, 2026

Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) that allows a single interface or method to perform different tasks. In Java, polymorphism is mainly classified into Compile-time Polymorphism and Run-time Polymorphism, depending on when the method to be executed is determined.

  • Improves code reusability and flexibility.
  • Supports method overloading and method overriding.
  • Makes programs easier to maintain and extend.

Compile-time Polymorphism

Compile-time polymorphism is a type of polymorphism in which the method to be executed is determined during compilation. It is achieved using method overloading, where multiple methods have the same name but different parameters.

  • Also known as Static Polymorphism or Early Binding.
  • Method selection happens at compile time.
Java
public class GFG {

    // First addition function
    public static int add(int a, int b)
    {
        return a + b;
    }

    // Second addition function
    public static double add(
        double a, double b)
    {
        return a + b;
    }

    // Driver code
    public static void main(String args[])
    {
        // Here, the first addition
        // function is called
        System.out.println(add(2, 3));

        // Here, the second addition
        // function is called
        System.out.println(add(2.0, 3.0));
    }
}

Output
5
5.0

Explanation: In this example, the add() method is overloaded with different parameter types. When add(2, 3) is called, the compiler selects the int version, and when add(2.0, 3.0) is called, it selects the double version. Since the method is chosen during compilation, this is called compile-time polymorphism.

Run-time Polymorphism

Run-time polymorphism is a type of polymorphism in which the method to be executed is determined during program execution. It is achieved using method overriding, where a child class provides its own implementation of a parent class method.

Java
// Implementing a class
class Test {

    // Implementing a method
    public void method()
    {
        System.out.println("Method 1");
    }
}

// Defining a child class
public class GFG extends Test {

    // Overriding the parent method
    public void method()
    {
        System.out.println("Method 2");
    }

    // Driver code
    public static void main(String args[])
    {
        Test test = new GFG();

        test.method();
    }
}

Output
Method 2

Explanation: In this example, the method() of the parent class Test is overridden in the child class GFG. Although the reference is of type Test, it points to a GFG object. At runtime, the JVM calls the overridden method() of the child class, printing "Method 2". This is called run-time polymorphism.

Compile-time Vs Run-time Polymorphism

The following table demonstrates the difference between runtime polymorphism and compile-time polymorphism:

FeatureCompile-time PolymorphismRun-time Polymorphism
Also Known AsStatic PolymorphismDynamic Polymorphism
Achieved ByMethod OverloadingMethod Overriding
Binding TimeCompile Time (Early Binding)Run Time (Late Binding)
Method SelectionDecided by the compilerDecided by the JVM during execution
Inheritance RequiredNoYes
Method SignatureSame method name with different parametersSame method signature in parent and child classes
Execution SpeedFasterSlightly slower due to dynamic binding
FlexibilityLess flexibleMore flexible
Decision Based OnMethod parametersActual object created at runtime
Exampleadd(int, int) and add(double, double)Parent reference calling overridden child method
Use CasePerforming similar operations with different inputsProviding different implementations of the same method


Comment