What is Polymorphism in Java?
Understand polymorphism in Java: compile-time overloading vs runtime overriding, dynamic dispatch, examples, and interview questions explained simply.
Expected Interview Answer
Polymorphism in Java is the ability of a single interface, reference, or method name to take many forms, so the same call behaves differently depending on the actual object or arguments involved.
Java offers two kinds. Compile-time (static) polymorphism is achieved through method overloading, where multiple methods share a name but differ in parameters. Runtime (dynamic) polymorphism is achieved through method overriding, where a superclass reference points to a subclass object and the JVM selects the overridden method at runtime via dynamic dispatch. This lets you write code against a general type while concrete subclasses supply the specific behaviour.
- One reference type can drive many implementations
- Enables flexible, extensible designs
- Reduces conditional branching on type
- Supports the open/closed principle
- Makes code easier to test and mock
AI Mentor Explanation
A captain shouts one instruction, bowl your best delivery, to every bowler. The pacer responds with a yorker, the spinner with a googly, the medium pacer with a swinging length ball. One command, many outcomes based on who receives it. Runtime polymorphism works the same: a call to deliver() runs whichever bowler subclass the reference actually points to.
Step-by-Step Explanation
Step 1
Declare a common type
Define a superclass or interface with a method that subtypes will implement or override.
Step 2
Override in subclasses
Each subclass provides its own version of the method using the @Override annotation.
Step 3
Reference by the base type
Store subclass objects in variables typed as the superclass or interface.
Step 4
Call the method
Invoke the shared method; the JVM uses dynamic dispatch to pick the right override at runtime.
Step 5
Overload for compile-time
For static polymorphism, define multiple methods with the same name but different parameter lists.
What Interviewer Expects
- Distinction between compile-time and runtime polymorphism
- Overloading versus overriding
- How dynamic dispatch selects the method
- Using a superclass or interface reference
- A concrete code example
Common Mistakes
- Treating overloading and overriding as the same thing
- Believing return type alone can overload a method
- Thinking static methods can be overridden polymorphically
- Forgetting that field access is not polymorphic in Java
- Confusing polymorphism with plain inheritance
Best Answer (HR Friendly)
“Polymorphism means one action can behave in many ways depending on the object performing it. In Java the same method call can run different code for different object types, which keeps programs flexible because you write against a general type and let each specific type fill in the details.”
Code Example
class Shape {
double area() {
return 0;
}
}
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
@Override
double area() {
return Math.PI * r * r;
}
}
class Square extends Shape {
double s;
Square(double s) { this.s = s; }
@Override
double area() {
return s * s;
}
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Circle(2), new Square(3) };
for (Shape shape : shapes) {
System.out.println(shape.area());
}
}
}Follow-up Questions
- What is the difference between overloading and overriding?
- How does the JVM implement dynamic dispatch?
- Can static methods be overridden in Java?
- Why is field access not polymorphic in Java?
- How do interfaces enable polymorphism?
MCQ Practice
1. Which mechanism provides runtime polymorphism in Java?
Method overriding lets a superclass reference invoke a subclass's version at runtime via dynamic dispatch.
2. Method overloading is resolved at?
Overloading is static polymorphism, resolved by the compiler based on the argument types.
3. Can two methods be overloaded by differing only in return type?
Overloaded methods must differ in parameter list; return type alone is not enough.
Flash Cards
Two types of polymorphism? — Compile-time (overloading) and runtime (overriding).
How is runtime polymorphism achieved? — Through method overriding and dynamic dispatch on a superclass or interface reference.
Is overloading resolved at runtime? — No — overloading is resolved by the compiler at compile time.
Are static methods polymorphic? — No — static methods are hidden, not overridden, so no dynamic dispatch occurs.