Java · OOP, JVM and backend craft
What is polymorphism and how does Java implement it?
Short answer
Polymorphism lets one reference type call behaviour that differs by runtime object. Java implements runtime polymorphism through dynamic dispatch of overridden methods.
There are two forms. Compile-time polymorphism is overloading: the compiler chooses a method from the static types and argument list. Runtime polymorphism is overriding: the JVM dispatches to the implementation of the actual object, even when the reference is the parent type.
abstract class Animal {
abstract String sound();
}
class Dog extends Animal {
@Override
String sound() {
return "woof";
}
}
Animal animal = new Dog();
System.out.println(animal.sound());Rules that affect dispatch
- An overriding method cannot reduce visibility
- static, private and final methods are not dynamically dispatched
- Return types may be covariant in an override
- Fields are hidden, not overridden, so access depends on the reference type
- Overload selection uses the compile-time types
Polymorphism is what makes interfaces useful. A caller can depend on a PaymentMethod and accept card, bank or wallet implementations without a type switch. The cost is indirection, but the JVM optimises monomorphic and bimorphic call sites effectively.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between overloading and overriding?
- 2Can a static method be overridden?
- 3What is covariant return type?
- 4Why are fields not polymorphic?
Say it in the interview
Polymorphism means a single reference type can invoke behaviour that depends on the actual object. Java gets runtime polymorphism from overriding and dynamic dispatch, so a parent reference to a Dog calls Dog's implementation. Overloading is compile-time polymorphism and is resolved from the static types. I use polymorphism to depend on interfaces and avoid type switches, while remembering that static, private and final methods do not participate in dynamic dispatch.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.