Polymorphism in Java — Compile-Time and Runtime
Fast recall
Quick revision
Core answer
Understanding of both types of polymorphism and what distinguishes them
Confusing overloading with overriding, or not knowing which one is compile-time vs runtime
Explain that runtime polymorphism is the more powerful and commonly used form — it's the foundation of OOP design patterns
Interview-ready guide
Interview answer
I'd start with the idea, not the dictionary: the same method call lands on different behavior depending on the object behind it. Java does this two ways, and the line interviewers care about is when the choice gets made.
Overloading is the compile-time kind — same name, different parameters, and the compiler picks the version from the arguments you pass before the program runs. Overriding is the runtime kind — a child rewrites a parent's method, and the JVM picks the version from the real object in memory while the program runs.
People mix these up constantly, so I keep one rule:
- Overloading — same class plus different parameters
- Overriding — parent-child plus the same signature
Of the two, overriding is the one that actually matters in design — it's the foundation under patterns like Strategy and Template Method, because you write code against the parent type and it keeps working for subclasses you add years later.
Polymorphism in Java — Compile-Time and Runtime
Direct Answer
Polymorphism lets code use one type or operation with different implementations. The common interview classification is compile-time polymorphism through overload resolution and runtime subtype polymorphism through overriding of eligible instance methods. Overloading is chosen from compile-time argument types; overriding dispatch is chosen from the runtime object's class.
Code Example
// ── Runtime polymorphism: method overriding ────────────────────────────────
class Animal {
// Parent defines the method
void speak() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Woof!"); } // child overrides
}
class Cat extends Animal {
@Override
void speak() { System.out.println("Meow!"); } // child overrides
}
// ── Compile-time polymorphism: method overloading ──────────────────────────
class Calculator {
// Same name, different parameter types — compiler picks at compile time
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
public class PolymorphismDemo {
public static void main(String[] args) {
// Runtime polymorphism — JVM looks at actual object at runtime
Animal a1 = new Dog(); // Animal reference, Dog object
Animal a2 = new Cat(); // Animal reference, Cat object
a1.speak(); // Output: Woof! ← JVM chose Dog.speak()
a2.speak(); // Output: Meow! ← JVM chose Cat.speak()
// Compile-time polymorphism — compiler picks from argument types
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // Output: 5 ← int version
System.out.println(calc.add(2.5, 3.5)); // Output: 6.0 ← double version
System.out.println(calc.add(1, 2, 3)); // Output: 6 ← 3-arg version
}
}
// Output:
// Woof!
// Meow!
// 5
// 6.0
// 6
Common Mistakes & Pitfalls
- Mixing up overloading and overriding — overloading is same class with different parameters; overriding is parent-and-child with the same signature. Swapping the two is the single most common fresher slip.
- Forgetting which one is compile-time — overloading is resolved by the compiler (static binding); overriding is resolved by the JVM at runtime (dynamic binding). Saying overriding happens at compile time is an instant red flag.
- Assuming all polymorphism needs inheritance — overriding does, but overloading lives inside one class and needs no parent at all.
- Thinking the reference type chooses the overridden method — for overriding, the actual object decides which version runs, never the declared variable type.
Check your understanding
Answer these without looking back at the article.
- How does runtime polymorphism work with a parent reference pointing to a child object?
- What is the difference between method overloading and method overriding?
- Can you achieve polymorphism without inheritance?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.