Unit 5 CS pt3
Unit 5 CS pt3
Introduction
The word polymorphism comes from Greek where polys means "many" and
morphe means "form or shape". In programming languages , polymorphism is the
provision of a single interface to entities of different types or the use of a single
symbol to represent multiple different types.
Following table shows gives a list of operators and their respective Python methods
for overloading.
>>>print(1 + 2)
Output
Output
pythonprogram
>>>print(3 * 4)
Output
12
Output
python pythonpythonpython
The mechanism works like this: If we have an expression "x + y" and x is an instance
of class K, then Python will check the class definition of K. If K has a method
__add__ it will be called with x.__add__(y), otherwise we will get an error message.
class A:
self.a = a
ob1 = A(1)
ob2 = A(2)
ob3 = A("binary")
ob4 = A("operator")
print(ob1 + ob2)
print(ob3 + ob4)
Output
Binaryoperator
Override means having two methods with the same name but doing different tasks.
It means that one of the methods overrides the other.
If there is any method in the superclass and a method with the same name in a
subclass, then by executing the method, the method of the corresponding class will
be executed.
We know that the child class inherits all the methods from the parent class
.However there will be situations where the definitions in parent class wont be
applicable to child class.In such cases we need to reform the method in the child
class.This process is known as method overriding.
Example 5
class A:
def foo(self):
class B(A):
def foo(self):
a1=A()
b1=B()
a1.foo()
b1.foo()
Output:
In the above example you can see that both parent class and child class have the
same name for the method i.e foo().The question that arises is as to which method
will get executed when the function is invoked. The answer to this is it depends on
the object that is invoking the function(method).If the child class invokes foo() then
the method in child class will be executed and if parent class object invokes foo()
the method in parent class gets executed.
Example 6
class A:
def explore(self):
class B(A):
def explore(self):
b = B()
a = A()
a.explore()
Output
In Python method overriding occurs by simply defining in the child class a method
with the same name of a method in the parent class.
Example 7
class Rectangle():
def __init__(self,length,breadth):
self.length = length
self.breadth = breadth
def getArea(self):
class Square(Rectangle):
def __init__(self,side):
self.side = side
Rectangle.__init__(self,side,side)
def getArea(self):
s = Square(4)
r = Rectangle(2,4)
s.getArea()
r.getArea()
Output
16 is area of square
8 is area of rectangle
Since the method from the coressponding class came into action, it means that one
overrode the other.Execution of 'getArea' on the object of Rectangle (r) printed "8
is area of rectangle" from the 'getArea' defined in the Rectangle class whereas,
execution of 'getArea' on the object of Square (s) printed "16 is area of square"
from the 'getArea' defined in the Square class.
If we still want to access the overridden method of the parent class in the child
class, that can be done using the super() function as follows:
Example 8
class A:
def foo(self):
class B(A):
def foo(self):
A.foo(self)
a1=A()
b1=B()
a1.foo()
b1.foo()
Output: