Introduction
Polymorphism is one of the four fundamental principles of Object-Oriented Programming (OOP), along with Encapsulation, Inheritance, and Abstraction. The word “Polymorphism” comes from two Greek words:
- Poly = Many
- Morph = Forms
Therefore, polymorphism means “many forms.”
In programming, polymorphism allows the same method, function, or operator to behave differently depending on the object or data it is working with. This makes programs more flexible, reusable, and easier to maintain.
Example: consider a sound() method. A dog barks, a cat meows, and a cow moos. Even though all animals use the same method name, the behavior differs based on the object.
What is Polymorphism in Python?
Polymorphism is the ability of an object, method, function, or operator to take multiple forms.
It allows the same interface to perform different actions depending on the context.
Example:
print(len("Python"))
print(len([1, 2, 3, 4]))
Output:
4
Explanation:
The same function:
len()
works differently for:
Strings
Lists
This is a simple example of polymorphism.
Types of Polymorphism in Python
Python mainly supports:
- Function Polymorphism
- Method Polymorphism
- Inheritance-Based Polymorphism
- Operator Overloading
1. Function Polymorphism
A single function can work with different object types.
Example: len()
print(len("Hello"))
print(len([1, 2, 3]))
print(len((10, 20, 30, 40)))
Output:
3
4
Explanation:
The same function behaves differently depending on the object type.
Another Function Polymorphism Example
print(max(10, 20))
print(max("A", "B"))
Output:
B
The same function works for numbers and strings.
2. Method Polymorphism
Different classes can have methods with the same name.
Example:
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
dog = Dog()
cat = Cat()
dog.sound()
cat.sound()
Output:
Meow
Although the method name is the same, the behavior changes.
Method Polymorphism Using Loops
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
Output:
Meow
This allows generic code to work with multiple object types.
3. Inheritance-Based Polymorphism
Inheritance and method overriding are common ways to achieve polymorphism.
Parent Class
class Animal:
def sound(self):
print("Animal Sound")
Child Classes
class Dog(Animal):
def sound(self):
print("Dog Barks")
class Cat(Animal):
def sound(self):
print("Cat Meows")
Usage:
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
Output:
Cat Meows
This is known as runtime polymorphism.
Runtime Polymorphism
Runtime polymorphism occurs when the method to execute is determined during program execution.
Example:
class Employee:
def work(self):
print("Employee Working")
class Manager(Employee):
def work(self):
print("Manager Managing")
class Developer(Employee):
def work(self):
print("Developer Coding")
Usage:
employees = [
Manager(),
Developer()
]
for employee in employees:
employee.work()
Output:
Developer Coding
Python decides which method to execute at runtime.
Duck Typing in Python
Python follows the concept of Duck Typing.
A famous statement is:
“If it walks like a duck and quacks like a duck, then it is a duck.”
In Python, object type is less important than behavior.
Example:
class Bird:
def fly(self):
print("Bird Flying")
class Airplane:
def fly(self):
print("Airplane Flying")
def start_flying(obj):
obj.fly()
start_flying(Bird())
start_flying(Airplane())
Output:
Airplane Flying
The function works with any object that contains a fly() method.
4. Operator Overloading
Operators can behave differently for different data types.
This is another form of polymorphism.
Example:
print(5 + 3)
print("Hello" + " World")
Output:
Hello World
The same operator + performs:
- Addition for numbers
- Concatenation for strings
Custom Operator Overloading
Python allows custom classes to overload operators.
Example:
class Number:
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.value + other.value
num1 = Number(10)
num2 = Number(20)
print(num1 + num2)
Output:
The + operator now works with custom objects.
Real-Life Examples:
1. Payment System
class CreditCard:
def pay(self):
print("Paid using Credit Card")
class PayPal:
def pay(self):
print("Paid using PayPal")
class UPI:
def pay(self):
print("Paid using UPI")
Usage:
payments = [
CreditCard(),
PayPal(),
UPI()
]
for payment in payments:
payment.pay()
Output:
Paid using PayPal
Paid using UPI
Explanation:
Different payment methods use the same interface.
2. Notification System
class Email:
def send(self):
print("Email Sent")
class SMS:
def send(self):
print("SMS Sent")
class PushNotification:
def send(self):
print("Push Notification Sent")
Usage:
notifications = [
Email(),
SMS(),
PushNotification()
]
for notification in notifications:
notification.send()
Output:
SMS Sent
Push Notification Sent
Polymorphism allows the same method call to handle different notification types.
Polymorphism vs Method Overriding
| Feature | Polymorphism | Method Overriding |
|---|---|---|
| Meaning | Same interface, different behavior | Child class redefines parent method |
| Scope | Broad OOP concept | Specific implementation |
| Inheritance Required | Not Always | Yes |
| Example | len(), + operator | Overridden methods |
| Purpose | Flexibility | Customization |
Method overriding is one way to achieve polymorphism.
Advantages of Polymorphism
| Advantage | Description |
|---|---|
| Reusability | Generic code works with multiple objects |
| Flexibility | Easy to add new classes |
| Scalability | Supports large applications |
| Cleaner Code | Reduces conditional logic |
| Maintainability | Easier updates and extensions |
Common Mistakes
1. Confusing Polymorphism with Inheritance
Polymorphism can exist without inheritance.
Example:
class Bird:
def fly(self):
pass
class Airplane:
def fly(self):
pass
This still demonstrates polymorphism.
2. Using Different Method Names
Incorrect:
class Dog:
def bark(self):
pass
class Cat:
def meow(self):
pass
Better:
class Dog:
def sound(self):
pass
class Cat:
def sound(self):
pass
3. Forgetting Required Methods
Incorrect:
class Bird:
pass
If fly() is expected, an error will occur.
4. Overusing Type Checking
Avoid:
if type(obj) == Dog:
Use polymorphism instead.
5. Not Leveraging Duck Typing
Python’s strength is behavior-based programming.
Focus on methods rather than object types.
Conclusion
Polymorphism is one of the most powerful concepts in Python’s Object-Oriented Programming model. It allows the same interface to perform different actions depending on the object being used.
Polymorphism improves code flexibility, reusability, maintainability, and scalability, making it an essential concept for building modern software applications. By mastering polymorphism, developers can write cleaner, more efficient, and highly extensible Python programs.
Python Polymorphism – Interview Questions
Q 1: What is polymorphism in Python?
Q 2: Can methods be overridden in polymorphism?
Q 3: What is operator overloading?
Q 4: What is method overloading in Python?
Q 5: How is polymorphism achieved in Python?
Python Polymorphism – Objective Questions (MCQs)
Q1. What does polymorphism mean in Python?
Q2. Which of the following best demonstrates polymorphism?
Q3. What will be the output of the following code?
class Cat:
def sound(self):
return "Meow"
class Dog:
def sound(self):
return "Bark"
for animal in [Cat(), Dog()]:
print(animal.sound())
Q4. What is method overriding in Python?
Q5. Which of the following is not an example of polymorphism in Python?