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

What is a Virtual Function?

Understand virtual functions, vtables, and dynamic dispatch in OOP and C++, plus how they differ from non-virtual static binding.

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

Expected Interview Answer

A virtual function is a member function declared in a base class that a derived class can override, with the actual implementation resolved at runtime based on the object’s real type rather than the reference or pointer type used to call it.

In languages like C++, marking a function virtual causes the compiler to generate a virtual table (vtable) entry for it, and calls through a base-class pointer or reference are dispatched through that table at runtime instead of being bound at compile time. Without the virtual keyword, C++ uses static binding, meaning the base class version runs even if the object is actually a derived type, which usually surprises developers coming from languages like Java where all instance methods are virtual by default. Pure virtual functions, declared with = 0, additionally make a class abstract, forcing every concrete derived class to supply an implementation.

  • Enables true runtime polymorphism through base pointers/references
  • Lets derived classes customize behavior safely
  • Pure virtual functions enforce abstract interfaces
  • Underpins extensible plugin and framework architectures

AI Mentor Explanation

A generic "bowl" instruction from the captain is only truly polymorphic if the team explicitly agrees every bowler will interpret it in their own specialist style — that agreement is like declaring the function virtual. Without that agreement, the captain’s exact literal instruction (the base implementation) is what everyone follows regardless of their specialty, no matter who is actually holding the ball, which mirrors non-virtual static binding in C++ where the base version always runs unless virtual is specified.

Step-by-Step Explanation

  1. Step 1

    Mark the base method virtual

    In C++, prefix the method with virtual in the base class declaration.

  2. Step 2

    Override in derived classes

    Redeclare the same signature in derived classes, ideally with the override specifier.

  3. Step 3

    Call through a pointer or reference

    Invoke the method via a Base* or Base& that actually points to a derived object.

  4. Step 4

    Runtime dispatch via vtable

    The compiler-generated virtual table looks up and calls the correct derived implementation at runtime.

What Interviewer Expects

  • Understanding of the virtual keyword and vtable mechanism in C++
  • Contrast with static/non-virtual binding
  • Knowledge of pure virtual functions and abstract classes
  • Awareness that Java/C# methods are virtual by default unless sealed/final

Common Mistakes

  • Forgetting a virtual destructor in a polymorphic base class, causing resource leaks
  • Assuming non-virtual functions still get polymorphic dispatch
  • Confusing pure virtual (= 0) with a regular virtual function
  • Not knowing virtual function calls have a small runtime overhead versus static calls

Best Answer (HR Friendly)

A virtual function is a method a base class explicitly allows subclasses to replace, so that calling it through a generic reference still runs the specific subclass’s version at runtime. It’s the mechanism, especially in C++, that makes true polymorphism work instead of always running the base class’s default behavior.

Code Example

Virtual function dispatch (C++-style concept shown in Java-like pseudocode)
class Shape {
    // In C++: virtual double area();
    double area() { return 0; }
}

class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    @Override
    double area() { return Math.PI * r * r; }
}

Shape s = new Circle(3);
System.out.println(s.area()); // dispatches to Circle's override at runtime

Follow-up Questions

  • What is a vtable and how does it enable dynamic dispatch?
  • Why should base class destructors be declared virtual in C++?
  • What is the difference between a virtual function and a pure virtual function?
  • Is there runtime overhead to calling a virtual function?

MCQ Practice

1. In C++, calling a non-virtual function through a base class pointer resolves to?

Without virtual, C++ uses static binding, so the base class implementation always runs through a base pointer.

2. A pure virtual function is declared with?

The = 0 syntax marks a pure virtual function, making the class abstract.

3. Why should a polymorphic base class have a virtual destructor?

Without a virtual destructor, deleting a derived object through a base pointer causes undefined behavior and resource leaks.

Flash Cards

What is a virtual function?A base class method that derived classes can override, resolved at runtime through a vtable.

What happens without virtual in C++?Static binding occurs — the base class version always runs through a base pointer/reference.

What is a pure virtual function?A virtual function declared with = 0 that has no base implementation and makes the class abstract.

Why need a virtual destructor?So deleting a derived object via a base pointer correctly runs the derived destructor first.

1 / 4

Continue Learning