← Back to Questions
Java

Difference between compile-time and runtime polymorphism?

Learn Difference between compile-time and runtime polymorphism? with simple explanations, real-time examples, interview tips and practical use cases.

Polymorphism in Java means:

One method behaving differently in different situations.

Java mainly supports two types of polymorphism:

  • Compile-Time Polymorphism
  • Runtime Polymorphism

Polymorphism Overview Diagram


Polymorphism

     |
     +-------> Compile-Time Polymorphism
     |
     +-------> Runtime Polymorphism


What is Compile-Time Polymorphism?

Compile-time polymorphism occurs when method execution is decided during compilation.

It is mainly achieved using:

  • Method Overloading

Compile-Time Polymorphism Example

class Calculator {

    int add(int a, int b) {

        return a + b;

    }

    double add(double a, double b) {

        return a + b;

    }

}

How It Works?

Compiler decides which method to call based on:

  • Method name
  • Parameter type
  • Parameter count

Compile-Time Polymorphism Flow


Method Call

     |
     v

Compiler Checks Parameters

     |
     v

Correct Overloaded Method Selected


Example

Calculator c = new Calculator();

c.add(10, 20);

c.add(10.5, 20.5);

Compiler selects correct method during compilation.


What is Runtime Polymorphism?

Runtime polymorphism occurs when method execution is decided during runtime.

It is mainly achieved using:

  • Method Overriding

Runtime Polymorphism Example

class Animal {

    void sound() {

        System.out.println("Animal Sound");

    }

}

class Dog extends Animal {

    @Override
    void sound() {

        System.out.println("Dog Barks");

    }

}

How It Works?

JVM decides which method to execute during runtime based on:

  • Actual object type

Runtime Polymorphism Flow


Parent Reference

     |
     v

Child Object Created

     |
     v

JVM Checks Object Type

     |
     v

Overridden Method Executes


Example

Animal a = new Dog();

a.sound();

Output

Dog Barks

Why Child Method Executes?

Because JVM checks actual object type during runtime.


Major Difference Diagram


Compile-Time Polymorphism

    |
    +-------> Method Decided by Compiler


Runtime Polymorphism

    |
    +-------> Method Decided by JVM at Runtime


Difference Between Compile-Time and Runtime Polymorphism

Feature Compile-Time Polymorphism Runtime Polymorphism
Achieved Using Method Overloading Method Overriding
Method Resolution During Compilation During Runtime
Inheritance Required No Yes
Performance Faster Slightly Slower
Binding Type Static Binding Dynamic Binding
Flexibility Less Flexible More Flexible
Example Method Overloading Method Overriding

Static Binding vs Dynamic Binding

Compile-Time Polymorphism

Uses:

Static Binding

Method call resolved during compilation.


Runtime Polymorphism

Uses:

Dynamic Binding

Method call resolved during runtime.


Real-Time Example of Compile-Time Polymorphism

E-commerce search functionality:

search(product)

search(product, category)

search(product, category, price)

Same method name with different parameters.


Real-Time Example of Runtime Polymorphism

Notification system:

sendNotification()

Different implementations:

  • EmailNotification
  • SMSNotification
  • PushNotification

Enterprise Banking Example

Compile-Time Polymorphism

calculateInterest(amount)

calculateInterest(amount, years)

Runtime Polymorphism

processPayment()

Different implementations:

  • UPIPayment
  • CreditCardPayment
  • NetBankingPayment

Compile-Time Polymorphism Internal Working


Method Call

      |
      v

Compiler Checks Signature

      |
      v

Exact Method Selected


Runtime Polymorphism Internal Working


Parent Reference

      |
      v

Object Created

      |
      v

JVM Determines Actual Object Type

      |
      v

Overridden Method Executes


Advantages of Compile-Time Polymorphism

  • Better performance
  • Faster execution
  • Improved readability
  • Flexible method usage

Advantages of Runtime Polymorphism

  • Supports extensibility
  • Enables dynamic behavior
  • Improves flexibility
  • Encourages reusable architecture

Disadvantages of Compile-Time Polymorphism

  • Limited flexibility
  • Cannot change behavior dynamically

Disadvantages of Runtime Polymorphism

  • Slight runtime overhead
  • Complex inheritance structures

Compile-Time Polymorphism in Spring Boot

Spring Boot uses compile-time polymorphism in:

  • Logger methods
  • RestTemplate methods
  • Repository methods

Runtime Polymorphism in Spring Boot

Spring Boot uses runtime polymorphism in:

  • Security configurations
  • Custom service implementations
  • Bean overriding
  • Strategy pattern implementations

Compile-Time vs Runtime Polymorphism Flow Diagram


Compile-Time Polymorphism

Code Written
     |
     v
Compiler Resolves Method
     |
     v
Execution


Runtime Polymorphism

Code Written
     |
     v
Object Created at Runtime
     |
     v
JVM Resolves Method
     |
     v
Execution


Common Interview Mistake

Many developers think both polymorphisms work the same way.

Actually:

  • Compile-time polymorphism is resolved by compiler
  • Runtime polymorphism is resolved by JVM during execution

Best Practices

  • Use overloading for logically similar methods
  • Use overriding for extensible architectures
  • Avoid unnecessary deep inheritance
  • Use polymorphism to reduce tight coupling

Related Learning Topics


Professional Interview Answer

Compile-time polymorphism in Java is achieved using method overloading, where multiple methods have the same name but different parameters. The compiler determines the correct method during compilation, so it is also called static binding. Runtime polymorphism is achieved using method overriding, where a child class provides its own implementation of a parent class method. The JVM determines the actual method during runtime based on object type, so it is also called dynamic binding. Compile-time polymorphism improves performance and readability, while runtime polymorphism improves flexibility and extensibility in enterprise applications and object-oriented architectures.


Frequently Asked Questions

What is compile-time polymorphism?

Compile-time polymorphism is achieved using method overloading and resolved during compilation.

What is runtime polymorphism?

Runtime polymorphism is achieved using method overriding and resolved during runtime.

Which polymorphism uses static binding?

Compile-time polymorphism uses static binding.

Which polymorphism uses dynamic binding?

Runtime polymorphism uses dynamic binding.

Which is faster?

Compile-time polymorphism is generally faster because method resolution happens during compilation.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.