


Khusboo Tayal
Polymorphism is one of the key principles of Object-Oriented Programming (OOP). It allows objects of different classes to be treated as objects of a common superclass. In Python, polymorphism provides flexibility and reusability by allowing the same method or function to behave differently based on the object that calls it.
This tutorial will help you understand what polymorphism is, how it works in Python, and how to use it in real-world applications.
The term Polymorphism is derived from Greek, meaning “many forms.”
In Python, polymorphism allows the same interface (method or function) to behave differently for different classes.
class Dog:
def speak(self):
return "Bark"
class Cat:
def speak(self):
return "Meow"Here, both classes have a speak() method, but they behave differently depending on the object.
Python supports two main types of polymorphism:
Python allows subclasses to provide a specific implementation of a method that is already defined in the parent class.
class Bird:
def fly(self):
print("Bird can fly")
class Sparrow(Bird):
def fly(self):
print("Sparrow flies at low altitude")
class Eagle(Bird):
def fly(self):
print("Eagle flies high")
for bird in (Sparrow(), Eagle()):
bird.fly()
Output:
Sparrow flies at low altitude Eagle flies high
The fly() method is the same name but behaves differently based on the object — this is polymorphism in action.
You can create functions that take different objects but call the same method.
def make_animal_speak(animal):
print(animal.speak())
class Dog:
def speak(self):
return "Bark"
class Cat:
def speak(self):
return "Meow"
make_animal_speak(Dog()) # Output: Bark
make_animal_speak(Cat()) # Output: Meow
Polymorphism also works with methods that share the same name across different classes.
class Shape:
def area(self):
pass
class Square(Shape):
def area(self):
return "Area = side * side"
class Circle(Shape):
def area(self):
return "Area = π * r * r"
shapes = [Square(), Circle()]
for shape in shapes:
print(shape.area())
Python uses a concept called duck typing: "If it looks like a duck and quacks like a duck, it’s a duck."
You don't need to explicitly inherit a class to use its methods — if an object implements the required behavior, it works.
class Duck:
def sound(self):
return "Quack"
class Human:
def sound(self):
return "Hello"
def make_sound(obj):
print(obj.sound())
make_sound(Duck()) # Output: Quack
make_sound(Human()) # Output: Hello
Python does not support traditional method overloading (multiple methods with the same name but different parameters), but you can simulate it using default arguments or variable arguments.
class Greet:
def hello(self, name=None):
if name:
print(f"Hello, {name}")
else:
print("Hello")
g = Greet()
g.hello() # Output: Hello
g.hello("John") # Output: Hello, John
Imagine you are building a payment system:
class Payment:
def pay(self):
pass
class CreditCard(Payment):
def pay(self):
return "Paid using Credit Card"
class PayPal(Payment):
def pay(self):
return "Paid using PayPal"
def process_payment(payment_method):
print(payment_method.pay())
process_payment(CreditCard())
process_payment(PayPal())
Polymorphism in Python is a powerful feature that allows functions and methods to work with different data types and objects using the same interface. It enhances flexibility, promotes clean code, and simplifies maintenance by allowing multiple classes to share the same method name with different implementations.
As you work on larger projects, using polymorphism will help you build more robust and scalable applications using object-oriented design.
Q1: Is polymorphism possible without inheritance in Python?
Yes, Python's dynamic typing and duck typing allow polymorphism even without inheritance.
Q2: What is the main advantage of polymorphism?
It lets the same method name perform different tasks based on the context, improving code flexibility and readability.
Q3: Does Python support method overloading like Java?
Not directly. Python methods can accept optional parameters or use *args and **kwargs to simulate overloading.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #pythonpolymorphism #objectorientedprogramming #pythonclasses #methodoverriding #ducktyping #pythontutorial #learnpython #pythonforbeginners