The next important component under the OOPs concept is Polymorphism in Python. In this article, let us see how it works and how it can be implemented in Python programs.
In general, polymorphism refers to the ability of a real-time object to represent itself in many forms. Consider yourself as a real-time example. When you are at college, you act as a student. When are at your home, you act like a child. You are a single person, but you represent yourself differently in different places.
Similarly in the OOPs concept, polymorphism refers to the ability of a method to process objects differently depending on their data type or class. Before understanding the working of polymorphism, let us discuss its usage.
Usage Of Polymorphism In Python
Polymorphism is used in Python programs when you have commonly named methods in two or more classes or subclasses. It allows these methods to use objects of different types at different times. So, it provides flexibility, using which a code can be extended and easily maintained over time.
Python allows us to define polymorphism in different ways. So, let us go ahead and see how it works.
Understanding Polymorphism In Python
As we already discussed, Polymorphism allows a method to process objects differently depending on their data type or class. First, let us see how a built-in method process objects having different data types.
Polymorphism In Built-In Method
In the below-given example, the built-in method ‘len’ processes two different objects, one being a list & another a string.
#printing length of two different objects
print(len([2, 4, 6, 8, 9]))
Here, the method ‘len’ prints the length of two different objects. In the first print statement, it prints the length of a list object and in the second, it prints the length of a string object. So, polymorphism is achieved using the built-in method ‘len’.
Next, let us see how a user-defined method process objects created for different user-defined classes.
Polymorphism In User-Defined Method
In the below example, ‘calculate_area’ is the user-defined method created with different operations in both the classes ‘Rectangle’ and ‘Circle’.
length = 5
breadth = 3
return self.length * self.breadth
radius = 4
return 3.14 * self.radius * self.radius
rec = Rectangle() #object created for the class 'Rectangle'
cir = Circle() #object created for the class 'Circle'
print("Area of a rectangle:", rec.calculate_area()) #call to 'calculate_area' method defined inside the class 'Rectangle'
print("Area of a circle:", cir.calculate_area()) #call to 'calculate_area' method defined inside the class 'Circle'
Area of a rectangle: 15
Area of a circle: 50.24
Here, polymorphism is achieved using the method ‘calculate_area’ that works differently in different classes.
In Python, polymorphism can be combined with Inheritance, i.e., polymorphism can also be achieved in inherited classes.
Polymorphism With Inheritance
But, before that have a look at the concept of Inheritance.
Polymorphism allows us to define a child class method with the same name as that of a parent class method. In the below example, ‘Car’ is the parent class and ‘RaceCar’ is the child class. A method named ‘drive’ is defined in both classes with different operations. So, polymorphism is achieved using the method ‘drive’.
def drive(self): #parent class method
print("I can drive at a speed of 60km/hr")
def drive(self): #child class method with the same name as that of the parent class
print("I can drive at a speed of 40km/hr")
maruti1 = Car()
maruti2 = RaceCar()
maruti1.drive() #accessing the parent class method
maruti2.drive() #accessing the child class method
I can drive at a speed of 60km/hr
I can drive at a speed of 40km/hr
Polymorphism In Python FAQs
Polymorphism in Python refers to the ability of a method to process objects differently depending on their data type or class.
Polymorphism in Python programs can be achieved by defining two or more methods with the same name inside two or more classes.
It provides flexibility so that any code can be extended and easily maintained over time.