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
- Give me a subclass that the compiler accepts but that still breaks the Liskov substitution principle. What rule does it break?mediumAlso on polymorphism5 min
- Where does encapsulation or polymorphism change a design, and how do you decide between composition and inheritance?mediumAlso on polymorphism6 min
- A method has been running fast for an hour and then gets slower, with no code change and no extra traffic. What might the runtime be doing?hardAlso on jit5 min
- What are the four pillars of OOP, and what is the difference between abstraction and encapsulation?easyAlso on polymorphism5 min