Friday, 21 April 2017

Polymorphism

Polymorphism:

  • the concept of defining multiple functionalities/logic's to perform one operation is known as a polymorphism.
  • poly means -----> many
  • morphism means ------> forms ( functionalities/logic's)


Polymorphism can be implemented in two ways,they are
  • method overloading ( static polymorphism )
  • method overriding ( dynamic polymorphism )

Method Overloading:

  • concept of defining multiple methods with same name with different number.of parameters is known as a method overloading or static polymorphism.
  • when ever we try to implement method overloading concept in python, by default python interpreter recognize recently defined method only.
  • python not support method overloading concept.
class x:
    def m1(self):
        print("i am in single parameter of m1 of class x")
    def m1(self,a):
        print("i am in two parameters of m1 of class x")

x_obj=x( )
x_obj.m1(100)
x_obj.m1( )

i am in two parameters of m1 of class x

Traceback (most recent call last):
  File "E:/python_practice/examples/methodoverloading.py", line 9, in <module>
    x_obj.m1()
TypeError: m1() missing 1 required positional argument: 'a'

  • some people tells python support method overloading with example.
  • i think it is not correct statement.
class x:
    def add(self,instanceOf,*args):
        if instanceOf == 'int':
            result=0
        if instanceOf=='str':
            result=' '
        for i in args:
            result=result+i
        print(result)
x_obj=x( )
x_obj.add('int',10,20,30)
x_obj.add("str",'siva','krishna')


60
sivakrishna

Note:
  • upto my knowledge above example is not a polymorphism.
  • polymorphism means  multiple logics to perform single operation.
  • but above program single logic to perform multiple operations.

Method Overriding:

  • the concept of defining multiple methods with same name with same number.of parameters, one is in super class and another one  is in sub class is known as a method overriding or dynamic polymorphism.
  • when ever super class method is overrided in its sub class always sub class method will be executed if sub class object is created. 
class x:
    def m1(self):
        print("i am in m1 of x class")
class y(x):
    def m1(self):
        print("i am in m1 of y class")
y_obj=y( )
y_obj.m1( )

x_obj=x( )
x_obj.m1( )

i am in m1 of y class
i am in m1 of x class

class x:
    def m1(self):
        print("i am in m1 of x class")
class y(x):
    def m1(self):
        print("i am in m1 of y class")
y_obj=y( )
y_obj.m1( )

i am in m1 of y class

Note: 
  • my requirement is to display both x class(super class) and y class(sub class) statements.
class x:
    def m1(self):
        print("i am in m1 of x class")
class y(x):
    def m1(self):
        super( ).m1( )
        print("i am in m1 of y class")
y_obj=y( )
y_obj.m1( )


i am in m1 of x class
i am in m1 of y class

super( ):
  • super( ) statement is used to call the super class methods or constructor through sub class method or constructor.
Constructor Overriding:
  • the concept of defining multiple constructors with same name with same number.of parameters, one is in super class and another one  is in sub class is known as a constructor overriding
class x:
    def __init__(self):
        self.a=100
class y(x):
    def __init__(self):
        self.b=200
y_obj=y()
print(y_obj.b)
print(y_obj.a)

200
Traceback (most recent call last):
  File "E:/python_practice/examples/constructor overriding.py", line 9, in <module>
    print(y_obj.a)
AttributeError: 'y' object has no attribute 'a'


class x:
    def __init__(self):
        self.a=100
class y(x):
    def __init__(self):
        self.b=200
        super().__init__()
y_obj=y()
print(y_obj.b)
print(y_obj.a)

200
100

No comments:

Post a Comment