100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Vtable (Virtual Method Table)?

Vtable explained — per-class function pointer tables, the vptr, and how overriding rewrites slots to enable runtime polymorphism.

hardQ131 of 226 in Object Oriented Programming Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

A vtable (virtual method table) is a per-class array of function pointers that maps each virtual method to its actual implementation, used by the runtime to perform dynamic dispatch — every object of a polymorphic class stores a hidden pointer to its class’s vtable, and a virtual call is resolved by indexing into that table at runtime.

When a class is compiled, the compiler builds one vtable per class containing a pointer to the correct implementation of each virtual method that class can call, in a fixed slot order shared across the hierarchy. Every instance of that class carries a hidden pointer, often called the vptr, that points to its class’s vtable, set during construction. A subclass that overrides a method gets its own vtable where that method’s slot points to the subclass’s implementation instead of the parent’s, while unoverridden slots still point to the inherited implementation. Calling a virtual method then becomes: follow the object’s vptr to its vtable, index into the correct slot, and jump to that function pointer — an indirect call that costs one extra memory dereference compared to a direct (early-bound) call. This is the standard mechanism in C++ and conceptually mirrors how the JVM resolves virtual method calls, though the JVM often uses inline caching for further optimization.

  • Explains exactly how runtime polymorphism is implemented at the machine level
  • Clarifies the small, constant performance cost of virtual calls
  • Explains why object layout includes a hidden pointer in vtable-based languages
  • Foundational for understanding diamond-inheritance and multiple-vtable complications in C++

AI Mentor Explanation

A cricket board keeps a laminated lookup card for each team listing, by role, exactly which specific player currently performs each duty — who bowls the death overs, who keeps wicket — and every player on the field silently carries a reference back to their team’s current card. When the captain calls for “the death-over bowler,” the umpire doesn’t reason about it; they just check the current card’s entry for that slot and point to whoever is listed there right now. That laminated card is the vtable: a fixed lookup table of role-to-player mappings, consulted by following a reference from the player back to their team’s current table.

Step-by-Step Explanation

  1. Step 1

    Compiler builds one vtable per polymorphic class

    Each virtual method gets a fixed slot; the class’s vtable holds a pointer to the correct implementation for each slot.

  2. Step 2

    Each object stores a hidden vptr

    When an object is constructed, its hidden vptr is set to point at its own class’s vtable.

  3. Step 3

    Overriding rewrites a slot, not the layout

    A subclass that overrides a method gets its own vtable where that slot points to the subclass’s implementation; the slot order stays identical across the hierarchy.

  4. Step 4

    A virtual call follows vptr, indexes, and jumps

    At the call site, the runtime dereferences the object’s vptr, indexes into the correct slot, and calls the function pointer found there.

What Interviewer Expects

  • A clear description of vtable as a per-class array of function pointers
  • Mention of the vptr (hidden pointer) stored per object, set at construction
  • Understanding that overriding replaces a slot’s pointer rather than restructuring the table
  • Awareness of the extra indirection cost versus a direct/early-bound call

Common Mistakes

  • Thinking there is one vtable per object rather than one per class (objects only hold a pointer to it)
  • Confusing the vtable with the object’s actual instance data (fields)
  • Forgetting that static, private, and non-virtual methods bypass the vtable entirely
  • Not mentioning that this is why polymorphic objects carry extra memory overhead (the vptr)

Best Answer (HR Friendly)

A vtable is basically a lookup table the language runtime builds for each class, listing exactly which function should run for every overridable method. Every object of that class carries a small hidden pointer to its class’s table, so when you call an overridden method, the runtime just follows that pointer, looks up the right entry, and jumps to the correct implementation — that’s the actual mechanism behind runtime polymorphism.

Code Example

Conceptual vtable behind virtual dispatch (illustrative, not real Java internals)
class Shape {
    double area() { return 0; }         // vtable slot 0: Shape.area
}

class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    @Override
    double area() { return Math.PI * r * r; } // Circle’s vtable slot 0 -> Circle.area
}

// Conceptually:
// Shape’s vtable   = [ Shape.area ]
// Circle’s vtable  = [ Circle.area ]   (overridden slot points to Circle’s version)

Shape s = new Circle(3.0);
// s.vptr -> Circle’s vtable -> slot 0 -> Circle.area()
double a = s.area(); // runtime follows vptr, indexes slot 0, calls Circle.area()

Follow-up Questions

  • Why does each object need a hidden vptr instead of storing the vtable directly?
  • How does multiple inheritance complicate vtables in C++?
  • What is the performance cost of a vtable lookup compared to a direct function call?
  • How does the JVM optimize virtual calls beyond a plain vtable lookup?

MCQ Practice

1. A vtable is best described as?

The vtable is built once per class and lists the correct function pointer for each virtual method slot.

2. What does an object’s hidden vptr point to?

All instances of a class share one vtable; each instance’s vptr points to that same shared table.

3. Which method type bypasses the vtable entirely?

Static methods are resolved at compile time by declared type and never go through vtable-based dispatch.

Flash Cards

Vtable in one line?A per-class array of function pointers mapping virtual methods to their implementations.

What is a vptr?A hidden pointer stored in every object, pointing to its class’s vtable.

What happens when a subclass overrides a method?The subclass’s vtable slot for that method is rewritten to point to the override.

Cost of virtual dispatch?One extra indirection: follow vptr, index the slot, jump to the function pointer.

1 / 4

Continue Learning