- Courses
- 💻Programming Languages
- 🐍Python
- Object‑Oriented Programming (OOP)
- Polymorphism
On this page
Polymorphism
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
What Polymorphism Means in OOP
Polymorphism allows different objects to respond to the same operation in ways appropriate to their specific type.
The term comes from Greek: poly (many) + morph (form).
In programming, it means:
- Different classes can define methods with the same name
- Objects can be used interchangeably if they share a common interface
- Code becomes more flexible, extensible, and easier to maintain
Polymorphism is a direct consequence of inheritance and method overriding, but it also works without inheritance as long as objects implement the same behavior.
Method Overriding as Polymorphism
When a subclass overrides a method from its parent class, each subclass provides its own version of the behavior.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Bark"
class Cat(Animal):
def speak(self):
return "Meow"
Using polymorphism:
animals = [Dog(), Cat(), Animal()]
for a in animals:
print(a.speak())
Each object responds according to its own implementation.
The loop does not need to know the specific type of each object.
Polymorphism Without Inheritance (Duck Typing)
Python supports duck typing, a dynamic form of polymorphism.
If an object implements the required method, it can be used—regardless of its class hierarchy.
class Duck:
def speak(self):
return "Quack"
class Person:
def speak(self):
return "Hello"
def make_speak(obj):
return obj.speak()
Both objects work:
make_speak(Duck())
make_speak(Person())
Python focuses on behavior, not type.
“If it walks like a duck and quacks like a duck, it’s a duck.”
Polymorphism Through Common Interfaces
A common interface is a set of method names shared across classes.
This is a powerful design technique for building interchangeable components.
Example: a drawing system with different shapes.
class Circle:
def draw(self):
return "Drawing a circle"
class Square:
def draw(self):
return "Drawing a square"
Using them polymorphically:
shapes = [Circle(), Square()]
for s in shapes:
print(s.draw())
Each object implements the same interface (draw), so they can be used uniformly.
Polymorphism in Real‑World Design
Polymorphism is essential in many real‑world scenarios:
- File‑like objects: anything with
.read()and.write()behaves like a file - Iterables: any object implementing
__iter__can be used in a loop - Numeric types: integers, floats, and custom numeric classes all support arithmetic
- Frameworks and libraries: user‑defined classes plug into predefined interfaces
Polymorphism allows systems to grow without rewriting existing code.
Designing for Polymorphism
To take advantage of polymorphism:
- Define clear, consistent method names across related classes
- Use inheritance when classes share structure and behavior
- Use duck typing when only behavior matters
- Avoid type checks (
isinstance) unless absolutely necessary - Write functions that operate on interfaces, not concrete classes
These principles lead to flexible, extensible designs.
Summary
In this lesson, you learned how polymorphism enables different objects to respond to the same operation in their own way. You explored method overriding, duck typing, common interfaces, and practical design strategies. Polymorphism is a cornerstone of object‑oriented programming and allows you to build systems that are modular, extensible, and easy to maintain.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Inheritance
Next
Encapsulation
- What Polymorphism Means in OOP
- Method Overriding as Polymorphism
- Polymorphism Without Inheritance (Duck Typing)
- Polymorphism Through Common Interfaces
- Polymorphism in Real‑World Design
- Designing for Polymorphism
- Summary
