LearnThatStack Ace your next interview
Object-Oriented Programming · question
Question 5 of 90

What is polymorphism, and what types of polymorphism do languages support?

beginner
← All Object-Oriented Programming questions
Re-explain

Polymorphism means code written against one name works with many types, and the right version runs automatically. A loop calling shape.area() gets the circle's formula for circles and the square's for squares.

Languages support several flavours:

  • Subtype polymorphism: a subclass or implementation supplies its own version, and the object picks at runtime.
  • Ad-hoc polymorphism: one name, several parameter lists, resolved when the code is compiled. Operator overloading is the same idea.
  • Parametric polymorphism: generics, where one List<T> works for every element type.
  • Duck typing: in dynamic languages, any object with the right method fits.

The gain is that adding a type does not mean editing existing branches, so old code stays untouched. The cost is indirection. Reading a call site no longer tells you which method actually runs.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Object-Oriented Programming One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • When you call shape.area() through a Shape variable, how does the runtime decide which version runs? It dispatches on the object's actual class, not the variable's declared type, typically through a per-class method table called a vtable.
  • How does compile-time polymorphism differ from runtime polymorphism? The compiler picks an overload from declared types, the runtime picks an override from the object. A Circle held as a Shape gets the Shape overload.
  • What must an overriding method honour so code written against the base type still works? Name Liskov substitution: accept at least what the base accepts, promise at least what it promises, and throw nothing callers don't expect.
  • How would you add a new payment type without editing the existing classes? One new class implements the payment interface, and callers that use only the interface stay as they are. The factory or wiring still changes.
  • When would you choose generics over taking a base-class parameter? Generics keep the exact type, so a List<Circle> returns Circles with no cast. Use subtyping when behaviour varies per type; a bounded T gets both.

What you can say

  1. Polymorphism means I write code against one name, it works with many types, and the right version runs automatically.
  2. If a loop calls shape.area(), circles get the circle formula and squares get the square formula, and the loop never asks which is which.
  3. First there's subtype polymorphism, where each subclass or interface implementation supplies its own version and the object picks it at runtime.
  4. Ad-hoc polymorphism is overloading, one name with several parameter lists where the compiler picks one, and operator overloading works the same way.
  5. Parametric polymorphism is generics, so one List<T> works for every element type, and dynamic languages add duck typing, where any object with the right method fits.
  6. The payoff is that adding a type doesn't mean editing old branches, and the cost is indirection, because a call site no longer tells you which method actually runs.

Weak answers to avoid

  • Stops at overriding and inheritance Subtype polymorphism is only one kind. Name overloading, generics and duck typing too, and say whether each is resolved at compile time or at runtime.
  • Confuses overloading with overriding Overloads differ by parameter list and the compiler picks one; overrides replace a parent's method and the object picks at runtime. Mixing them up is a red flag.
  • Only says 'many forms' Knowing what the word means shows no understanding. Show one call site running different code per type, then name the kinds and what they cost.
  • Presents it as all gain, no cost Polymorphism adds indirection, since a call site no longer shows which method runs. Name that cost next to the gain of adding types without editing old code.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Object-Oriented Programming cheatsheet.

← Back to all Object-Oriented Programming questions
Pro · $10/mo

81 of 90 Object-Oriented Programming answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime