Table of Contents
In this tutorial, we will go through polymorphism concepts in python with examples.
Polymorphism is made up of two words:
poly means many and
morphism means several different forms. Like many other Object Oriented Programming Languages python also has the polymorphism concept. In this concept same object can perform in variety of ways based on the input we provide. It can be achieved by using
method overloading and
method overriding in python. Check more about polymorphism concept on Polymorphism in Python.
Polymorphism Concept in Python with Best Working Examples
Polymorphism is one of
OOPS concept which allows the inherited class to define/redefine the functions/methods that has the same name in base class.
Method overloading and
Method overriding are two types of polymorphism which proofs the concept. We will see both of them one by one below.
Method Overloading in python is defined as functions having same name identified by types of argument and number of arguments passed.
Unlike any other programming languages (like C++), it's implementation is different. Analyze below program. it will fail in python, however same will execute successfully in
C++ (Make syntax changes if executing in
def perimeter (a, b): p = 2*(a + b) print("Perimeter of rectangle is:" ,p) def area (a, b, c): p = 4*(a+b+c) print("Perimeter of cuboid is:",p) perimeter(3,4,5) #perimeter(1,4)
Now, try below ways to achieve Method overloading in python.
Program Example 1
In this example we are defining a method add() which take data type and different number of data as an argument. Then we are using a for loop to go through each of the data and sum of those values. Finally
add() method will display those values on the output. Here we are calling
add() method 3 times and each time with different data type and different number of arguments proving the concept of method overloading.
def add(type, *arg): if type =='int': sum = 0 if type =='str': sum = '' if type =='float': sum = 0.0 for x in arg: sum = sum+ x print(sum) add('int', 2,3) add('float', 1.0,2.6,3.4) add('str', 'Hi' , '\tCyberithub')
[root@localhost ~]# python example.py 5 7.0 Hi Cyberithub
Program Example 2
In this example also we are defining
add() method which takes different types of data and different number of arguments but this time we are using a
Class Operation and calling
add() method by creating object of this class.
class Operation: def add(self, sum=None, a=None, b=None): if sum == 'str' and a!=None and b!=None: print("String is:", (a+b)) elif sum == 'float' and a!=None and b!=None: print("Float Multiplication is:", (a*b)) elif sum == 'int' and a!=None and b!=None: print("Integer Addition is:", (a+b)) else: print("Invalid Input") ob= Operation() ob.add('str', 'Hi', "\tThere") ob.add('int', 2,6) ob.add(3,4.5) ob.add('float', 3.6, 4.4)
[root@localhost ~]# python example.py ('String is:', 'Hi\tThere') ('Integer Addition is:', 8) Invalid Input ('Float Multiplication is:', 15.840000000000002)
In Method Overriding, name of the function/method as well as parameters passed to that function are same in parent and child classes. So basically method overriding means having two methods with the same name but performs different tasks.
It comes handier when implementing
Inheritance in python. As you might be aware that in Inheritance, child class inherits the methods of Parent class but sometimes one cannot use the inherited methods as it is in child class. We need to modify the implementation of the method before use. This concept is Known as
Method Overriding. It is used for reusability purpose, hence reduces the memory usage while processing.
Program Example 1
In this example we have a method
Class Fruits which is getting overridden in another
Mango by showing different statement on the output as you can see in below python program.
class Fruits: def fruit(self): print("Fruits are sweet in taste") class Mango(Fruits): def fruit(self): print("Mangoes are sweet in taste") ob =Mango() ob.fruit()
[root@localhost ~]# python example.py Mangoes are sweet in taste
Program Example 2
Like above example, here we have a method
car() defined in
Class Car which subsequently getting overridden in another
Class Nissan by showing different statement on the output. This proves the concept of
class Car: def car(self): print("Car is used for Travel") class Nissan(Car): def car(self): print("Nissan is a very fast car") ob = Nissan() ob.car()
[root@localhost ~]# python example.py Nissan is a very fast car