Topics

Polymorphism

Welcome to the tutorial on Polymorphism in Python. In OOP, Polymorphism is a concept that refers to multiple forms or more than one form. It allows the use of a single interface with the input of different data types, and different classes or it could be for a different number of inputs.

In python language, everything is an object, as such by default a function can take anything as an argument, however, the execution of the function might fail as every function has some logic that it follows. Check the example below:

len("hello")      # returns 5 as result
len([1,2,3,4,45,345,23,42])     # returns 8 as result

From the above example, the len function is polymorphic, because it takes a string as input in the initial case, and it is taking list as input in the second case. Polymorphism is a way of making a function accept objects of different classes if they behave similarly in Python.

In Python, Method Overriding is a type of polymorphism whereby a child class that is extending the parent class can provide a different definition to any function defined in the parent class as per its own requirements.

 

Method Overloading

Method overriding, also referred to as function overloading is a type of polymorphism whereby we can define a number of methods with the same name, but with a different number of parameters, and the parameters can be of different types. However, these methods are capable of performing a similar or different function.

Note that, Python does not support method overloading based on a different number of parameters in functions.

 

Defining Polymorphic Classes

Now, visualize a case in which there is a different class for shapes such as Triangles, Squares, and so on, that we serve as a resource to calculate the area of that shape. We all know that each shape has a different number of dimensions that are used to calculate the area of the respective shape.

Thus, we can define different functions with different names to calculate the area of the given shapes. Check the example below:

class Square:
    side = 5     
    def calculate_area_sq(self):
        return self.side * self.side

class Triangle:
    base = 5
    height = 4
    def calculate_area_tri(self):
        return 0.5 * self.base * self.height

sq = Square()
tri = Triangle()
print("Area of square: ", sq.calculate_area_sq())
print("Area of triangle: ", tri.calculate_area_tri())

Output:

Area of square: 25
Area of triangle: 10.0

From the above example, you will notice that the implementation of the two classes (i.e. Square and Triangle) has the function with the same name calculate_area(). However, because of different objects, its call gets resolved correctly, that is when the function is called using the object sq; thereafter, the function of class Square is called and when it is called using the object tri then the function of class Triangle is called.

class Square:
    side = 5     
    def calculate_area(self):
        return self.side * self.side

class Triangle:
    base = 5
    height = 4
    def calculate_area(self):
        return 0.5 * self.base * self.height

sq = Square()
tri = Triangle()
print("Area of square: ", sq.calculate_area())
print("Area of triangle: ", tri.calculate_area())

Output:

Area of square: 25
Area of triangle: 10.0

 

Polymorphism with Class Methods

In the example above, what we observed is an obvious behavior. Now, we will use a loop that iterates over a tuple of objects of various shapes and call the area function to calculate the area for each shape object. Check out the example below:

sq = Square()
tri = Triangle()

for(obj in (sq, tri)):
    obj.calculate_area()

This example is a good example of the use of Polymorphism because we are treating objects of different classes as an object on which the same function gets called. The Python language does not care about the type of object that is calling the function, therefore, making the class method polymorphic.

 

Polymorphism with Functions

This is quite similar to the way we used loop in the previous example, can as well create a function that is capable of taking an object of some class as input and thereafter calling the function to calculate the area for the shape. Check out the example below:

find_area_of_shape(obj):
    obj.calculate_area()

sq = Square()
tri = Triangle()

# calling the method with different objects
find_area_of_shape(sq)
find_area_of_shape(tri)

As you will notice, we used the same function i.e. find the area of the shape to calculate the area of two different shape classes. As such, a similar function takes different class objects as arguments and executes perfectly to return the result, and this is what is called Polymorphism.