Scrivio Interview library
InternalsIntermediate8 min read · updated 12 November 2026

How does the JVM implement virtual method calls, and how does the JIT make them cheap in common cases?

The JVM implements invokevirtual with a per-class virtual method table, the vtable, where each overridable method has a fixed slot, so a call loads the receiver's class, reads the slot and jumps; invokeinterface uses interface tables, itables, which need an extra search for the interface first. The JIT makes most calls cheap by profiling each call site: if it sees one or two receiver types, it inlines those methods behind a quick type-check guard, and class hierarchy analysis lets it inline a method that has only one loaded implementation, deoptimizing if another class loads later. Sites with many receiver types, megamorphic sites, fall back to table dispatch and cannot be inlined, while final and private methods need no dispatch at all.

Why this question is asked

We write interfaces and overridable methods everywhere in Java, and in hot code someone eventually asks what each of those calls costs. The honest answer is: usually nothing, sometimes a lot, and the difference comes from how the JVM and its JIT compiler handle virtual calls. Interviewers ask this internals question to see whether we understand dispatch tables, profiling and inlining well enough to explain a performance surprise, such as a loop that slowed down when a third implementation was added. In this answer, we will recap virtual calls, look at vtables and itables, follow how the JIT turns most calls into direct inlined code, see what makes a call site megamorphic, and cover class hierarchy analysis and deoptimization.

1A Quick Recap of Virtual Calls

A virtual call is a call whose target depends on the run-time class of the receiver, the object before the dot. In shape.area(), the code that runs depends on whether shape is a Circle or a Square. In bytecode, a call through a class type compiles to invokevirtual, and a call through an interface type compiles to invokeinterface. Both instructions name a method, Shape.area()D, and the JVM must find the right implementation each time the call runs.

Inlining is the key to understanding the cost. Inlining means the JIT, the just-in-time compiler that turns hot bytecode into machine code, copies the body of the called method into the caller. The call disappears, and, more importantly, the combined code can be optimised as one piece: constants folded, objects kept in registers, bounds checks removed. A call that cannot be inlined blocks all of that.

2Vtables and Itables

Each loaded class has a vtable, an array of pointers to method code. A subclass's vtable starts with a copy of its superclass's entries in the same order, then replaces the entries it overrides and appends new ones. Because the slot number of a method is the same in every subclass, the JVM can resolve invokevirtual to a fixed index once, and every call becomes: load the receiver's class pointer, load the entry at that index, jump to it.

Java
class Shape { double area() { return 0; } String name() { return "shape"; } }
class Circle extends Shape { double r; @Override double area() { return Math.PI * r * r; } }
// conceptual vtables (slot numbers are illustrative)
// Shape : [0] Object methods... [5] Shape.area [6] Shape.name
// Circle: [0] Object methods... [5] Circle.area [6] Shape.name <- same slots, area replaced
double a = shape.area(); // load shape's class -> read slot 5 -> call it
What a call costs, from cheapest to most expensive
Call siteWhat the JIT doesInlined?
final, private or static methoddirect call, one possible targetyes, if small enough
one implementation loaded (CHA)direct call, recorded dependencyyes, no guard
monomorphic profile (1 type seen)type-check guard, then inlined bodyyes
bimorphic profile (2 types seen)two guards, both bodies inlinedyes
megamorphic, one dominant typeguard for the dominant type, else table callonly the dominant one
megamorphic, no dominant typevtable or itable dispatchno

Interfaces cannot work this way, because one class can implement many interfaces and the same interface method has no common slot across all implementing classes. So each class also has itables: for each interface it implements, a small table of that interface's methods. An invokeinterface call must first search the receiver class's list of interfaces for the right one, then index into its table. HotSpot speeds this up with stubs and caching, but in the general case interface dispatch does a little more work than class dispatch.

3How the JIT Makes Most Calls Cheap

While a method runs in the interpreter and in the first-tier compiler, HotSpot profiles it. For each virtual call site it records which receiver classes it has seen. When the method becomes hot and the optimizing compiler, C2, compiles it, it reads that profile and specialises each call site.

Java
// source
double total = 0;
for (Shape s : shapes) total += s.area(); // profile: 100% Circle
// what C2 effectively generates for a monomorphic site
for (Shape s : shapes) {
if (s.getClass() != Circle.class) uncommonTrap(); // guard: one pointer comparison
Circle c = (Circle) s;
total += Math.PI * c.r * c.r; // Circle.area() inlined
}
Step throughHow C2 compiles a virtual call site
  1. Only one implementation loaded?class hierarchy analysis
    YesInline directly, record dependencyNoRead the call-site profile
  2. One or two receiver types seen?monomorphic or bimorphic
    YesInline behind type guardsNoMegamorphic site
  3. One type dominates (about 90%)?check your JDK's thresholds
    YesInline it, table call for the restNoPlain vtable or itable call
  4. Guard fails or new subclass loadsdeoptimize, back to interpreter, recompile later

4 steps in order. Play them, or step through them one at a time.

The guard compares the object's class pointer with Circle, which costs about one instruction. If the guard ever fails, the code takes an uncommon trap: it jumps back to the interpreter, and the method may be recompiled with a new profile. With two receiver types the JIT emits two guards and inlines both bodies; this is a bimorphic site. Either way, the virtual call has disappeared, and the loop is now ordinary arithmetic the compiler can optimise further.

4Megamorphic Call Sites

A call site is megamorphic when the profile shows many receiver types, more than the two that C2 inlines with guards. If one type still dominates, HotSpot can inline that one and use a table call for the rest. If no type dominates, the JIT emits a real vtable or itable dispatch. The dispatch itself costs only a few nanoseconds, but the bigger loss is that nothing behind the call can be inlined, so optimisations such as escape analysis, which keeps short-lived objects off the heap, stop at that call.

This explains a classic surprise. A benchmark of shapes.forEach(s -> total += s.area()) runs fast with circles and squares, and suddenly slows down when triangles are added, even though triangle area is trivial. The code did not get more work; the call site went from bimorphic to megamorphic. Notice also that a profile belongs to a call site in a method, so a shared helper called from many places with many types can be megamorphic even if each caller uses only one type.

5Class Hierarchy Analysis and Deoptimization

The JIT also knows which classes are loaded right now. Class hierarchy analysis, CHA, checks whether a method has been overridden by any loaded class. If only one implementation of Shape.area() exists, the call can be inlined with no guard at all, because there is nothing else it could call. This is why a non-final method with no overrides is usually just as fast as a final one.

This is an optimistic bet, so the JIT records a dependency: this compiled code assumes area() has a single implementation. If a class that overrides area() is loaded later, perhaps by a plugin or a lazily loaded module, the JVM deoptimizes the dependent code: it throws away the compiled version, sends any running frames back to the interpreter, and recompiles later with the new knowledge. This is the reason the JVM can be aggressive without being wrong.

Final and private methods are the simple end of the scale. A final method cannot be overridden and a private method is never dispatched through the subclass, so each has exactly one possible target and the JVM binds the call directly. The same holds for static methods and constructors.

6Follow-up questions

How could you detect a megamorphic call site in a hot path?

Start with a profiler that shows JIT stubs, such as async-profiler: frames named vtable stub or itable stub in a hot path are a direct sign of real table dispatch. To see the JIT's decisions, run with -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining, or produce a compilation log with -XX:+LogCompilation and read it in a tool such as JITWatch; call sites that were not inlined are reported with a reason, though the exact wording varies by JDK version. Finally, confirm with a JMH benchmark that feeds the call site one, two and then many receiver types and measures the difference.

Should we make methods final for performance?

Usually not. Class hierarchy analysis already treats a method with one loaded implementation as if it were final, and profiles handle the common one- or two-type cases. Final is a design decision, saying a method must not be overridden, and should be used for that reason. The performance-relevant question is how many receiver types reach a hot call site.

Are interface calls slower than class calls?

Only when they end up as real dispatch. After the JIT inlines a monomorphic or bimorphic site, an interface call costs the same as a class call: a guard and inlined code. In megamorphic sites, itable dispatch does a little more work than vtable dispatch, but both block inlining, which is the larger cost.

7Conclusion

In this answer, we saw that the JVM dispatches invokevirtual through vtables with fixed slots and invokeinterface through itables that need an extra interface search. The JIT avoids most of that cost by profiling call sites and inlining monomorphic and bimorphic sites behind cheap type guards, by using class hierarchy analysis to inline methods with only one loaded implementation, and by deoptimizing when a new class breaks an assumption. Megamorphic sites fall back to table dispatch and block inlining, and final and private methods need no dispatch at all.

What to say in the interviewAbout 100 seconds aloud

Open with

For invokevirtual, every class has a vtable, an array of method pointers where each overridable method has the same slot in the class and all its subclasses, with overrides replacing the entry.

Then, if they want more

So a call is: load the receiver's class, read the slot, jump. For invokeinterface it uses itables, because a class can implement many interfaces and there is no single common slot; the JVM first finds the right interface's table for the receiver class and then indexes into it, which is a bit more work.

In practice the JIT avoids most of this. HotSpot profiles each call site and records the receiver types it sees. If a site is monomorphic or bimorphic, C2 inlines the method bodies behind a cheap class-pointer guard, and if the guard fails it deoptimizes.

Class hierarchy analysis goes further: if only one implementation of a method is loaded, it inlines it with no guard and records a dependency, and if a class overriding it is loaded later, the compiled code is deoptimized and recompiled. Megamorphic sites, with many receiver types and no dominant one, fall back to a real vtable or itable call.

The call itself is a few nanoseconds, but it blocks inlining and everything inlining enables, like escape analysis. Final, private and static methods have one target, so they need no dispatch. To find megamorphic sites I would look for vtable or itable stubs in async-profiler and check PrintInlining output.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does the JVM implement virtual method calls, and how does the JIT make them cheap in...": https://scrivio-interview-library.netlify.app/java/oop-principles/7d865c733f/