Skip to content
Preptima
mediumConceptMidSenior

When you call an overridden method through a base-class reference, how does the runtime know which implementation to run?

5 min readUpdated 2026-08-15

Answer

Short answer

The compiler cannot know, so it emits an indirect call through a per-class table of function pointers that the object carries a reference to.The lookup itself is a couple of memory accesses; the real cost is the optimisations the compiler cannot perform when the target is not known until run time.

Was this answer helpful?

Likely follow-ups

  • Why can a JVM often make a virtual call as fast as a direct one, and when does that stop working?
  • Why is calling an overridable method from a constructor dangerous?
  • How does interface dispatch differ from class dispatch, and why is it harder?
  • What does `final` or `sealed` let a compiler do that it otherwise cannot?

Related questions

Related Topics