What is polymorphism in OOP?
Polymorphism means one name taking many forms, so the same method call can behave differently depending on the object. It comes in two types. Compile time polymorphism is achieved through method overloading, and run time polymorphism is achieved through method overriding. It lets you write flexible code that works with many related types through one interface.
- Polymorphism means one name taking many forms, so the same call behaves differently per object.
- Compile time polymorphism uses overloading, and run time polymorphism uses overriding.
- It lets you write code against a general type and add new types without changing old code.
The simple meaning
The word polymorphism comes from many forms. In code it means you can use a single method name and the program decides which actual behaviour to run based on the object involved. This keeps your code clean and open to new types.
Compile time polymorphism
This is achieved with method overloading. Several methods share a name but take different parameters, and the compiler picks the right one before the program runs.
int area(int side) { return side * side; }
int area(int l, int b) { return l * b; }int area(int side) { return side * side; }
int area(int l, int b) { return l * b; }# Python has no compile time overloading.
# A default argument covers both cases.
def area(l, b=None):
return l * l if b is None else l * bRun time polymorphism
This is achieved with method overriding. A parent reference can point to a child object, and the child version of the method runs at run time. This is the powerful form used in real designs.
class Shape {
public:
virtual void draw() { cout << "drawing shape"; }
};
class Circle : public Shape {
public:
void draw() override { cout << "drawing circle"; }
};
Shape* s = new Circle();
s->draw(); // prints drawing circleclass Shape {
void draw() { System.out.println("drawing shape"); }
}
class Circle extends Shape {
@Override
void draw() { System.out.println("drawing circle"); }
}
Shape s = new Circle();
s.draw(); // prints drawing circleclass Shape:
def draw(self):
print("drawing shape")
class Circle(Shape):
def draw(self): # override
print("drawing circle")
s = Circle()
s.draw() # prints drawing circleWhy it matters
Polymorphism lets you write code against a general type like Shape and add new shapes later without touching the old code. This is the foundation of flexible and extendable software.
A clean interview answer names both types and gives one example each. Say overloading is compile time and overriding is run time, then show a Shape and Circle example for the run time case, since that is the one interviewers care about most.
Frequently asked questions
What is run time polymorphism with an example?
A Shape reference pointing to a Circle object calls the Circle draw method at run time. The actual object type decides the behaviour, not the reference type.
How is polymorphism related to inheritance?
Run time polymorphism depends on inheritance, since a child must override a parent method for a parent reference to call the child version.
Common follow up questions
Related interview questions
Want the full OOP guide?
Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the OOP guide All OOP questions