Polymorphism in Python (With Example)

 Polymorphism 

               Polymorphism refers to having several different forms . It allows programmers to assign different meanings to a variable ,objects or method with the same name . Polymorphism is one of the important features of OOP. In python Polymorphism is simply means defining a number of subclass that have methods of the same name .

Polymorphism in Python



Types Of Polymorphism :

Polymorphism in Python

In Python there are different ways to provide Polymorphism like Method Overloading and  Operator Overloading also Method Overriding .

Method Overloading : 


      Method Overloading means having a method of same name but different numbers 
arguments. In other programming language like JAVA it support Method overloading 
directly . and compiler overload Method in which number of argument is same as passed
arguments .But python doesn't support Method overloading directly . so to use Method
overloading in python we have to use a concept called as default argument 

EXAMPLE : 


class calculation:
   def add(self, a=None ,b=None ,c=None):
       if a!=None and b!=None and c!=None :
         d= a +b +c
         print(d)


       elif a!=None and b!=None:
           d= a+ b
           print(d)


       else :
           d=a
           print(d)


obj=calculation()
obj.add(5,6,7)
obj.add(5,6)
obj.add(5)


Output :
18
11
5


Polymorphism in Python


  In above example we can pass different number of argument for same method  and getting 
expected Output .in this way we can implement Method overloading in Python .



NOTE
None in python is similar to null in c++ and java 

Method Overriding :

         Method overriding is Nothing but having two methods with same name and same number
of argument . A same class cannot have two methods of same name and same number of 
argument  so here we have to use a concept of Inheritance .a subclass can have a Method like
 that .

Example :



class greeting:
   def show(self):
       print("Hello")
class greet(greeting):
    def show(self):
        print("good morning")
obj =greet()
obj.show()
Output : good Morning

 In above example we have two classes in which greet inherits greeting .in both class we 
have method show but compiler is calling subclass method this what method overriding 
means. method of superclass is overridden by subclass method . but if we don't have that
method in subclass  . then the compiler will print super class method .




class greeting:
   def show(self):
       print("Hello")

class greet(greeting):
   pass  #no statements

obj =greet()
obj.show()

Output : Hello


Operator Overloading :

   Like Method overloading operator overloading also a form of compile time polymorphism 
In operator overloading different operators have different implementation depending on their 
argument .with operator overloading a programmer is allow to provide his own definition for
an operator by overloading Built in data type .
Example : 

Program to overload + operator on a complex object .



class Complex :
   def __init__(self):
       self.r=0
       self.i=0
   def set(self,r,i):
       self.r=r
       self.i=i
   def __add__(self, C):
       obj = Complex()
       obj.r=self.r+C.r
       obj.i=self.i+C.i
       return obj
   def display (self):
    print("(",self.r,"+",self.i,"i)")
obj1=Complex()
obj1.set(5,9)
obj2=Complex()
obj2.set(2,2)
obj3=Complex()
obj3=obj1+obj2
obj3.display()


Output 7+11i
In above program we use " + " operator to add obj1 and obj 2, we had define __add__()
Method which overload the inbuilt add operator .
We can also overload other operators but for that we must know function name for that 
Operator which are given below 
           

Operator
Function name
Operator 
Function name
          +

           -

           *

          /

         **

         %

          >>

          &

           l
          
          ^

          <<

__add__

__sub__

__mul__

__truediv__

__pow__

__mod__

__rshift__

__and__

__or__

__xor__

__lshift__
        =+

        -=

        *=

        /=

        **=

        %=

        >>=

         &=

           l=
          
          ^=

          <<=
__iadd__

__isub__

__imul__

__itruediv__

__ipow__

__imod__

__irshift__

__iand__

__ior__

__ixor__

__ilshift__