Polymorphism in Python refers to the ability of different classes to share the same method name but exhibit different behaviors. This is a key feature of Object-Oriented Programming (OOP) that enhances code flexibility and reusability.
For example, in real life, a teacher and a student both have a speak() method, but they use it differently—a teacher explains concepts, while a student asks questions.
Python achieves polymorphism in two main ways:
Method overriding allows a child class to provide a different implementation of a method already defined in its parent class.
Different animals make different sounds, but they all have a make_sound() method.
class Animal:
def make_sound(self):
print("Some generic animal sound")
class Dog(Animal):
def make_sound(self): # Overriding parent method
print("Bark! Bark!")
class Cat(Animal):
def make_sound(self): # Overriding parent method
print("Meow! Meow!")
# Creating objects
dog = Dog()
cat = Cat()
dog.make_sound() # Output: Bark! Bark!
cat.make_sound() # Output: Meow! Meow!Here, both Dog and Cat override the make_sound() method of the Animal class to provide their own implementation.
super()Sometimes, we may want to reuse the parent method while adding extra functionality. This is where super() comes in.
class Employee:
def work(self):
print("Completing assigned tasks.")
class Manager(Employee):
def work(self):
super().work() # Calling the parent method
print("Managing the team and projects.")
# Creating object
mgr = Manager()
mgr.work()Completing assigned tasks.
Managing the team and projects.The Manager class first calls the work() method of Employee and then adds additional behavior.
Polymorphism allows the same function to work with different objects.
class Car:
def fuel_type(self):
return "Petrol or Diesel"
class ElectricCar:
def fuel_type(self):
return "Electric Battery"
# Function using Polymorphism
def vehicle_info(vehicle):
print(f"Fuel type: {vehicle.fuel_type()}")
# Calling function with different objects
vehicle_info(Car()) # Output: Fuel type: Petrol or Diesel
vehicle_info(ElectricCar()) # Output: Fuel type: Electric BatteryThe vehicle_info() function works with both Car and ElectricCar objects because they share the fuel_type() method.
Python follows duck typing, which means an object’s behavior determines its usability rather than its type.
class CreditCard:
def make_payment(self):
print("Payment made using Credit Card.")
class UPI:
def make_payment(self):
print("Payment made using UPI.")
# Function that works with any payment method
def process_payment(payment_method):
payment_method.make_payment()
# Using different objects
process_payment(CreditCard()) # Output: Payment made using Credit Card.
process_payment(UPI()) # Output: Payment made using UPI.Here, the process_payment() function works with any object that has a make_payment() method, regardless of its class.
Python does not support method overloading natively, but we can achieve a similar effect using default parameters.
class Shape:
def area(self, length, breadth=None):
if breadth is None: # If only one parameter is given, calculate square area
return length * length
else: # If two parameters are given, calculate rectangle area
return length * breadth
shape = Shape()
print(shape.area(5)) # Square -> Output: 25
print(shape.area(5, 10)) # Rectangle -> Output: 50Here, the area() method works for both squares (one parameter) and rectangles (two parameters).
Method Overriding allows a child class to redefine a method from its parent class.
super() can be used to call the parent class’s method while extending it.
Polymorphism enables the same method name to work with different object types.
Duck Typing ensures an object is used based on its behavior, not its class.
Method Overloading is simulated using default arguments.
Sign in to join the discussion and post comments.
Sign inPython Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.
Python for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.