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:
- Compile time Polymorphism
- Runtime Polymorphism
Compile time polymorphism:
This type of polymorphism is achieved by function overloading or operator 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
def product(a, b):
p = a * b
# Second product method
# Takes three argument and print their
def product(a, b, c):
p = a * b*c
# Uncommenting the below line shows an error
=> Gives error
# This line will call the second product method
product(4, 5, 5)
We can overload the function in a different way.
def sayHello(self, name=None):
if name is not None:
print 'Hello ' + name
print 'Hello '
# Create instance
obj = Human()
# Call the method
# 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.
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
def __init__(self, radius):
self.__radius = radius
def setRadius(self, radius):
self.__radius = radius
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__
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
This type of polymorphism is achieved by 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.
self.length = length
self.breadth = breadth
print self.length*self.breadth," is area of rectangle"
self.side = side
print self.side*self.side," is area of square"
s = Square(4)
r = Rectangle(2,4)
Like this post
Share this post on
About the author
- Career over Relationships. Family above everything else.
- Passionate Geek with love for Computer Science, Artificial Intelligence.
- Avid interest in Economics, Sports and Adventures.
- Curious to know outside the boundary limits, coz what I know is very little and what I don’t know is huge.
Join the discussion