Polymorphism in Python
Polymorphism:
Poly means many and Morphism means forms. If something exhibits more than one form, it is called Polymorphism. A variable may store different types of data, and object may exhibit different behaviours in different contexts and also a method may perform various tasks.
a=10
print(a)
a= “Hello”
print(a)
c=Child()
c.read() //its method of parent
c.read()//its method of child
a=10
b=20
c=a+b
here, the + operator is act as addition of two integer numbers
a= “Bharathi”
b= “Devi”
c=a+b
here, the + operator is act as concatenating two strings
Operator overloading:-
Any operator performs additional actions other than what it is meant for is called operator overloading
Eg:-
print("using '+' operator to add two integers")
a=10
b=20
print(a+b)
print("using '+' operator to concatenate two strings")
a="bharathi"
b="devi"
print(a+b)
print("using '+' operator to append two lists")
a=[10,20,30]
b=[40,50,60]
print(a+b)
output:-
using '+' operator to add two integers
30
using '+' operator to concatenate two strings
bharathidevi
using '+' operator to append two lists
[10, 20, 30, 40, 50, 60]
Here, the + operator is first adding two integer numbers. Then the same operator is concatenating two strings. Finally the same operator is combining two lists of elements. But the + operator is not used to add two objects that is obj1+obj2. Now this can be achieved by overloading the operator “+”. The + operator act upon the two objects and perform addition operation on the contents of the objects. That means we are giving additional task to the ‘+’ operator. This comes under operator overloading.
The ‘+’ operator is in fact internally written as a special method i.e., __add__(). So, if we add two numbers by writing a+b, the internal method called
Ø a.__add__(b).
Ø a.__sub__(b)
Ø a.__mul__(b)
#program for operator overloading
class Tcs:
def read(self):
self.x=int(input("Enter number of employee"))
def display(self):
print(self.x)
def __add__(self,other):
return self.x+other.x
v=Tcs()
h=Tcs()
v.read()
h.read()
print("Employees in Tcs @ Vijayawada branch: ")
v.display()
print("Employee in Tcs @ Hyderabad branch: ")
h.display()
print("Total number of employees in TCS: ",v+h)
v.add(h)
Output:-
Enter number of employee10
Enter number of employee20
Employees in Tcs @ Vijayawada branch:
10
Employee in Tcs @ Hyderabad branch:
20
Total number of employees in TCS: 30
Method Overloading:-
If a method is written such that it can perform more than one task, it is called Method Overloading.
According to java program, Method names are same but differ with their parameter list is nothing method overloading.
Class MethodOverloading{
int add(int a,int b)
{
return(a+b)
}
int add(int a,int b,int c)
{
return(a+b+c)
}
int add(int a,int b,int c,int d)
{
return(a+b+c+d)
}
}
class MethodOverloadingEx
{
MethodOverloading m=new MethodOverloading();
System.out.println(“sum of two numbers:”+m.add(10,20));
System.out.println(“Sum of three numbers:”+m.add(10,20,30));
System.out.println(“Sum of four numbers:”+m.add(10,20,30,40));
}
This can not be available in python.
def add(a,b):
return(a+b)
def add(a,b,c):
return(a+b+c)
def add(a,b,c,d):
return(a+b+c+d)
print("Sum of two numbers: ",add(10,20))
print("Sum of three numbers: ",add(10,20,30))
print("Sum of four numbers: ",add(10,20,30,40))
PVM throws an error as follows
add() missing 2 required positional arguments: 'c' and 'd'
Writing more than one method with the same is not possible in python. So, we can achieve method overloading by writing same method with several parameters out of which some parameters assigned as None. The method performs the operation depending upon the number of arguments passed in the method call.
Method Overriding
When there is a method in the super class, writing the same method in the sub class so that it replaces the super class is called method overriding.
Let us take an example,
class Parent:
def show(self):
print("This is parent method")
class Child(Parent):
def show(self):
print("This is Child method")
c=Child()
c.show()
o/p:- This is Child method
When you execute this program, the control goes to only child class because the sub class replaces the super class method. If you wan to access the method of super class then use super() method to invoke the super class methods from the sub class method.
class Parent:
def show(self):
print("This is parent method")
class Child(Parent):
def show(self):
super().show()
print("This is Child method")
c=Child()
c.show()
o/p:- This is parent method
This is Child method
Comments
Post a Comment