Polymorphism concepts in python with example

By

  • Polymorphism word is made Greek words poly(many) and morphism(shape/forms).
  • It means we can use same method/function name  for different types.
  • We generally uses two types of polymorphism in c++,java ,c# etc.But python support only one polymorphism that is called run-time polymorphism.
Types of Polymorphism :-
  1. Compile time polymorphism (e.g. method overloading)
  2. Run-time polymorphism (e.g. method overriding)
  1.) Compile time polymorphism:- 
  • It is used for method overloading.
  • It support compile-time binding.Python  does not support compile time polymorphism.
  • In this, class ,method and object are bind at compile time.
Method Overloading in Python:-
Python does not support method overloading.It is not possible to define more than one method with the same name in a class in python.Because method arguments/parameters in python do not have a type.suppose we want to call a method that has one argument ,in python it can be integer or string,float or double. we already know,method argument in python do not have a type.
e.g.
class math:
    def add(self,a,b):
        c=a+b
        print("Addition of two Number:",c)
    def add(self,a,b,c):
        d=a+b+c
        print("Addition of three Number:",d)
#creating the object of the class
m=math()
#m.add(2,3)
m.add(5,4,6)
Output:-
Addition of three Number: 15
>>> 
Description:-
  • The problem of method overloading in python,we can access only the last method.Here add method run which takes three argument and last method of the program also.
  • When we call same add method which takes two arguments,it shows an error message. 
To achieve Method overloading in python:-
There are different ways to achieve method overloading in python.But we will discuss one of them.
  1. By using normal ways(Not more efficient)
  2. By using Variable arguments (efficient)
  3. By using Multiple Dispatch Decorator (efficient)
   1.) By using Variable arguments:-By using variable arguments,we can achieve method overloading concepts in python.
e.g.
class math:
    def add(self,*n):
        sum=0
        for num in n:
            sum=sum+num
        return sum
#creating the object of the class
m=math()
#method with four argument
sum1=m.add(3,4,5,6)
print("sum of four arguments="+str(sum1))
sum2=m.add(10,20,30)
print("sum of three arguments="+str(sum2))
sum3=m.add(40,50)
print("sum of two arguments="+str(sum3))
Output:-
sum of four arguments=18
sum of three arguments=60
sum of two arguments=90
>>> 
Note:- We will learn Multiple Dispatch Decorator concepts in our coming lectures.
  2.) Run-time polymorphism:-
  • It involves deciding at runtime which function from base class should get called.
  • Python supports runtime polymorphism. Python supports method overriding and operator overloading.
Polymorphism in python:-
  • A derived /child class inherits all the methods/functions from the parent/base class.
  • Suppose a child class inherits the method from base class.but this method is not quit fit in child class .In such case we will have to re-implement the same method in child class by adding some extra features.
There are three ways to implement polymorphism concepts in python.
  1. Polymorphism with function and objects
  2. Polymorphism with class methods
  3. polymorphism with inheritance
   1.) Polymorphism with function and objects:-
We can create a function/method that can take any object.That is allowing polymorphism.
e.g.
class Apple():
    def type(self):
        print("This is a fruit")
    def color(self):
        print("Apple color is red")
class Banana():
    def type(self):
        print("Banana is a fruit")
    def color(self):
        print("Banana color is yellow")
class onion():
    def type(self):
        print("Onion is a vegetable")
    def color(self):
        print("Onion color is purple")
def function1(obj):
    obj.type()
    obj.color()
#creating the object of the class
obj1=Apple()
obj2=Banana()
obj3=onion()
function1(obj1)
function1(obj2)
function1(obj3)
Output:-
This is a fruit
Apple color is red
Banana is a fruit
Banana color is yellow
Onion is a vegetable
Onion color is purple
>>> 
  2.) Polymorphism with class methods:-
Python uses two different class types in the same way.Here we will implement the polymorphism concepts with the help of for loop that iterates through a tuple of objects.
e.g.
class student():
    def display(self):
        print("I am a student")
    def language(self):
        print("This student speak English")
class employee():
    def display(self):
        print("I am an employee")
    def language(self):
        print("This employee speak Hindi")
st_obj=student()
em_obj=employee()
for person in (st_obj,em_obj):
    person.display()
    person.language()
Output:-
I am a student
This student speak English
I am an employee
This employee speak Hindi
>>> 
  3.) Polymorphism with Inheritance:-
Polymorphism defines methods in derived class that have the same name as the method in the parent class.In Inheritance,we know that the child class inherits the methods from the parent or base class.It is also possible to modify a method in a derived class that has inherited from the parent class.We already know,some times ,the method inherited from the parent or base class  doesn't fit the derived class.
This process to modify a method in the child class is known as method overriding.
e.g.

class grand_father:
    def height(self):
        print("My grandfther height is 6 fit")
    def skin_color(self):
         print("My grandfather skin color is white")
class father(grand_father):
    def height(self):
        print("My father height is 7 fit")
    def skin_color(self):
         print("My father skin color is black")
class son(grand_father):
    def height(self):
        print("This height is 6.56 fit")
    def skin_color(self):
         print("This son skin color is black")          
obj_grandfather=grand_father()
obj_father=father=father()
#calling the class members
obj_son=son()
obj_grandfather.height()
obj_grandfather.skin_color ()
obj_father.height()
obj_father.skin_color()
obj_son.height()
obj_son.skin_color()
Output:-
My grandfther height is 6 fit
My grandfather skin color is white
My father height is 7 fit
My father skin color is black
This height is 6.56 fit
This son skin color is black
>>> 
Watch complete Lecture 39 video:-


For More...

0 comments:

Post a Comment

Powered by Blogger.