Overview of Polymorphism

Polymorphism, meaning many forms, is the ability of the same method call to behave differently depending on the actual object it is invoked on, so code can work with related types through one shared interface. Compile-time polymorphism, such as function overloading, is resolved by the compiler, while runtime polymorphism, such as method overriding, is resolved during execution.

3 min read

Polymorphism, literally "many forms," is the ability for the same method call to produce different behavior depending on which specific object it's actually called on. It's what lets you write code against a general type (like Shape) while still getting the correct, specific behavior for whatever concrete object (Circle, Square, Triangle) is actually being used at runtime.

The two kinds of polymorphism

Compile-time polymorphism (static binding): resolved by the compiler before the program even runs. Function overloading (multiple functions with the same name but different parameters) is the classic example.

Runtime polymorphism (dynamic binding): resolved while the program is actually running, based on the real type of the object, not the type of the reference/pointer used to access it. Method overriding through inheritance and virtual functions is the classic example.

A worked example

#include <iostream>
using namespace std;

class Shape {
public:
    virtual double area() {
        return 0;
    }
};

class Circle : public Shape {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() override { return 3.14159 * radius * radius; }
};

class Square : public Shape {
    double side;
public:
    Square(double s) : side(s) {}
    double area() override { return side * side; }
};

void printArea(Shape* s) {
    cout << "Area: " << s->area() << endl;
}

int main() {
    Circle c(5);
    Square sq(4);

    printArea(&c);   // calls Circle's area(), even though the parameter type is Shape*
    printArea(&sq);  // calls Square's area()
    return 0;
}

printArea() only knows about Shape*, but it correctly calls Circle::area() for the circle and Square::area() for the square. This is runtime polymorphism: the actual method that executes depends on the real object's type, resolved at runtime, not on the pointer's declared type.

Where this matters in real systems

Every plugin architecture depends on this. A media player that supports MP3, WAV, and FLAC files typically defines a common AudioDecoder interface with a decode() method, and the player's core logic calls decoder->decode() without needing to know or care which specific format it's dealing with, the correct decoding logic runs automatically based on the real object passed in. This is exactly what lets new file formats be added later just by writing a new class that implements the same interface, with zero changes to the player's core code.

What interviewers actually ask about polymorphism

  • "What's the difference between compile-time and runtime polymorphism?" Compile-time polymorphism (like function overloading) is resolved by the compiler before the program runs; runtime polymorphism (like method overriding via virtual functions) is resolved while the program is executing, based on the object's actual type.
  • "How does runtime polymorphism actually work under the hood?" Through a virtual function table (vtable), each object with virtual methods carries a hidden pointer to a table of function addresses specific to its real class, and calls are dispatched through that table at runtime instead of being hardcoded at compile time.
  • "Why is polymorphism useful?" It lets code be written against a general interface or base class while still getting correct, type-specific behavior, making it easy to add new types later without changing the code that uses them.
  • "Give a real example of polymorphism you've used or seen." Any plugin-style or strategy-pattern design: a payment system calling processPayment() on a general PaymentMethod interface, correctly running CreditCard, PayPal, or BankTransfer-specific logic depending on the real object passed in.

Key takeaways

  • Polymorphism lets the same method call behave differently based on the actual object it's invoked on.
  • Compile-time polymorphism (overloading) is resolved before runtime; runtime polymorphism (overriding) is resolved during execution.
  • Runtime polymorphism in C++ works through virtual functions and a vtable.
  • It's the foundation of plugin architectures and any design where new types need to be added without changing existing calling code.
All 72 OOP interview questions

Log in to save which topics you've read across every subject.

Frequently asked questions

What's the difference between compile-time and runtime polymorphism?

Compile-time polymorphism (like function overloading) is resolved by the compiler before the program runs; runtime polymorphism (like method overriding via virtual functions) is resolved while the program is executing, based on the object's actual type.

How does runtime polymorphism actually work under the hood?

Through a virtual function table (vtable), each object with virtual methods carries a hidden pointer to a table of function addresses specific to its real class, and calls are dispatched through that table at runtime instead of being hardcoded at compile time.

Why is polymorphism useful?

It lets code be written against a general interface or base class while still getting correct, type-specific behavior, making it easy to add new types later without changing the code that uses them.

Give a real example of polymorphism you've used or seen.

Any plugin-style or strategy-pattern design: a payment system calling `processPayment()` on a general `PaymentMethod` interface, correctly running CreditCard, PayPal, or BankTransfer-specific logic depending on the real object passed in.