Polymorphism in Python

Polymorphism in Python

June 13, 2018
6 minutes read time
695 times viewed

header image


The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism, a person at a same time can have different characteristic. Like a man at a same time is a father, a husband, a employee. So a same person posses have different behavior in different situations. This is called polymorphism.


Polymorphism is considered as one of the important features of Object Oriented Programming.

Polymorphism is mainly divided into two types:

  1. Compile time Polymorphism
  2. Runtime Polymorphism

Compile time polymorphism:

This type of polymorphism is achieved by function overloading or operator overloading.

Function Overloading

When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.

Python does not supports method overloading. We may overload the methods but can only use the latest defined method.

# First product method.
# Takes two argument and print their
# product
def product(a, b):
    p = a * b

# Second product method
# Takes three argument and print their
# product
def product(a, b, c):
    p = a * b*c

# Uncommenting the below line shows an error    

product(4, 5)

=> Gives error

# This line will call the second product method
product(4, 5, 5)

=> 100

We can overload the function in a different way.

class Human:

    def sayHello(self, name=None):

        if name is not None:
            print 'Hello ' + name
            print 'Hello '

# Create instance
obj = Human()

# Call the method

=> Hello

# Call the method with a parameter

=> Hello Astik

In the above example, a single function named sayHello() acts differently in 2 different situations which is the property of polymorphism.

Operator Overloading

There is also an option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add to operands. So a single operator ‘+’ when placed between integer operands , adds them and when placed between string operands, concatenates them.

Python Operator Overloading

import math

class Circle:

    def __init__(self, radius):
        self.__radius = radius

    def setRadius(self, radius):
        self.__radius = radius

    def getRadius(self):
        return self.__radius

    def area(self):
        return math.pi * self.__radius ** 2

    def __add__(self, another_circle):
        return Circle( self.__radius + another_circle.__radius )

c1 = Circle(4)

c2 = Circle(5)

c3 = c1 + c2 # This became possible because we have overloaded + operator by adding a    method named __add__

Expected Output:


In the above example we have added __add__ method which allows use to use + operator to add two circle objects. Inside the __add__ method we are creating a new object and returning it to the caller. Similarly we can do for others like __mul__, __sub__ etc.

Runtime Polymorphism

This type of polymorphism is achieved by Function Overriding.

Function Overriding

Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

class Rectangle():
    def __init__(self,length,breadth):
        self.length = length
        self.breadth = breadth
    def getArea(self):
        print self.length*self.breadth," is area of rectangle"
class Square(Rectangle):
    def __init__(self,side):
        self.side = side
    def getArea(self):
        print self.side*self.side," is area of square"

s = Square(4)
r = Rectangle(2,4)

=> 16

=> 16


We are done

Like this post

1   Like

Share this post on

Google Plus LinkedIn

About the author

Join the discussion

Nothing to preview

Post Comment