Ticker

6/recent/ticker-posts

Python: Polymorphism

Poly means many. Morphs mean forms.

Polymorphism means 'Many Forms'.

Eg1: Yourself is the best example of polymorphism. In front of Your parents You will have one type of behavior and with friends another type of behavior. Same person but different behaviors at different places, which is nothing but polymorphism.

Eg2: + operator acts as concatenation and arithmetic addition

Eg3: * operator acts as multiplication and repetition operator

Eg4: The Same method with different implementations in Parent class and child 

classes.(overriding)

Related to polymorphism the following 4 topics are important

1. Duck Typing Philosophy of Python

2. Overloading

  1.  Operator Overloading
  2.  Method Overloading
  3.  Constructor Overloading

3. Overriding

  1.  Method overriding
  2.  constructor overriding

1. Duck Typing Philosophy of Python:

In Python we cannot specify the type explicitly. Based on provided value at runtime the type will be considered automatically. Hence Python is considered as Dynamically Typed Programming 

Language.

def f1(obj):
 obj.talk()

What is the type of obj? We cannot decide at the beginning. At runtime we can pass any type.Then how we can decide the type?

At runtime if 'it walks like a duck and talks like a duck,it must be duck'. Python follows this principle. This is called Duck Typing Philosophy of Python.

Demo Program:

1) class Duck: 
2) def talk(self): 
3) print('Quack.. Quack..') 
4) 
5) class Dog: 
6) def talk(self): 
7) print('Bow Bow..') 
8) 
9) class Cat: 
10) def talk(self): 
11) print('Moew Moew ..') 
12) 
13) class Goat: 
14) def talk(self): 
15) print('Myaah Myaah ..') 
16) 
17) def f1(obj): 
18) obj.talk() 
19) 
20) l=[Duck(),Cat(),Dog(),Goat()] 
21) for obj in l: 
22) f1(obj) 
Output:
Quack.. Quack..
Moew Moew ..
Bow Bow..
Myaah Myaah ..

Overloading:

We can use same operator or methods for different purposes.

Eg1: + operator can be used for Arithmetic addition and String concatenation
 print(10+20)#30
 print('durga'+'soft')#durgasoft
Eg2: * operator can be used for multiplication and string repetition purposes.
 print(10*20)#200
 print('durga'*3)#durgadurgadurga
Eg3: We can use deposit() method to deposit cash or cheque or dd
 deposit(cash)
 deposit(cheque)
 deposit(dd)

There are 3 types of overloading

  1. Operator Overloading
  2. Method Overloading
  3. Constructor Overloading

1. Operator Overloading:

We can use the same operator for multiple purposes, which is nothing but operator overloading.

Python supports operator overloading.

Eg1: + operator can be used for Arithmetic addition and String concatenation
 print(10+20)#30
 print('durga'+'soft')#durgasoft
Eg2: * operator can be used for multiplication and string repetition purposes.
 print(10*20)#200
 print('durga'*3)#durgadurgadurga

Demo program to use + operator for our class objects:

1) class Book: 
2) def __init__(self,pages): 
3) self.pages=pages 
4) 
5) b1=Book(100) 
6) b2=Book(200) 
7) print(b1+b2) 
D:\durga_classes>py test.py
Traceback (most recent call last):
 File "test.py", line 7, in <module>
 print(b1+b2)
TypeError: unsupported operand type(s) for +: 'Book' and 'Book'

We can overload + operator to work with Book objects also. i.e Python supports Operator Overloading.

For every operator Magic Methods are available. To overload any operator we have to override that Method in our class. 

Internally + operator is implemented by using __add__() method.This method is called magic method for + operator. We have to override this method in our class. 

Demo program to overload + operator for our Book class objects:

1) class Book: 
2) def __init__(self,pages): 
3) self.pages=pages 
4) 
5) def __add__(self,other): 
6) return self.pages+other.pages 
7) 
8) b1=Book(100) 
9) b2=Book(200) 
10) print('The Total Number of Pages:',b1+b2) 
Output: The Total Number of Pages: 300

2. Method Overloading:

If 2 methods having same name but different type of arguments then those methods are said to be overloaded methods.

Eg: m1(int a)
 m1(double d)

But in Python Method overloading is not possible.

If we are trying to declare multiple methods with same name and different number of arguments then Python will always consider only last method.

Demo Program:

1) class Test: 
2) def m1(self): 
3) print('no-arg method') 
4) def m1(self,a): 
5) print('one-arg method') 
6) def m1(self,a,b): 
7) print('two-arg method') 
8) 
9) t=Test() 
10) #t.m1() 
11) #t.m1(10) 
12) t.m1(10,20) 
Output: two-arg method

In the above program python will consider only last method.

How we can handle overloaded method requirements in Python:

Most of the times, if method with variable number of arguments required then we can handle with default arguments or with variable number of argument methods.

Demo Program with Default Arguments:

1) class Test: 
2) def sum(self,a=None,b=None,c=None): 
3) if a!=None and b!= None and c!= None: 
4) print('The Sum of 3 Numbers:',a+b+c) 
5) elif a!=None and b!= None: 
6) print('The Sum of 2 Numbers:',a+b) 
7) else: 
8) print('Please provide 2 or 3 arguments') 
9) 
10) t=Test() 
11) t.sum(10,20) 
12) t.sum(10,20,30) 
13) t.sum(10) 
Output:
The Sum of 2 Numbers: 30
The Sum of 3 Numbers: 60
Please provide 2 or 3 arguments

3. Constructor Overloading:

Constructor overloading is not possible in Python.

If we define multiple constructors then the last constructor will be considered.

1) class Test: 
2) def __init__(self): 
3) print('No-Arg Constructor') 
4) 
5) def __init__(self,a): 
6) print('One-Arg constructor') 
7) 
8) def __init__(self,a,b): 
9) print('Two-Arg constructor') 
10) #t1=Test() 
11) #t1=Test(10) 
12) t1=Test(10,20) 
Output: Two-Arg constructor

In the above program only Two-Arg Constructor is available.

But based on our requirement we can declare constructor with default arguments and variable 

number of arguments.

Constructor with Default Arguments:

Constructor with Variable Number of Arguments:

1) class Test: 
2) def __init__(self,*a): 
3) print('Constructor with variable number of arguments') 
4) 
5) t1=Test() 
6) t2=Test(10) 
7) t3=Test(10,20) 
8) t4=Test(10,20,30) 
9) t5=Test(10,20,30,40,50,60) 
Output:
Constructor with variable number of arguments
Constructor with variable number of arguments
Constructor with variable number of arguments
Constructor with variable number of arguments
Constructor with variable number of arguments

Method overriding:

What ever members available in the parent class are bydefault available to the child class through inheritance. If the child class not satisfied with parent class implementation then child class is allowed to redefine that method in the child class based on its requirement. This concept is called overriding.

Overriding concept applicable for both methods and constructors.

Demo Program for Method overriding:

1) class P: 
2) def property(self): 
3) print('Gold+Land+Cash+Power') 
4) def marry(self): 
5) print('Appalamma') 
6) class C(P): 
7) def marry(self): 
8) print('Katrina Kaif') 
9) 
10) c=C() 
11) c.property() 
12) c.marry() 
Output:
Gold+Land+Cash+Power
Katrina Kaif

"Python Polymorphism"

"Python Polymorphism Overloading"

"Python Polymorphism Constructor"

"Python Polymorphism Overriding"

"Python Polymorphism Inheritance"

"Oop Python Polymorphism"

"Define Python Polymorphism"

"Python Class Polymorphism"

"Python Inheritance And Polymorphism"