0% found this document useful (0 votes)
15 views9 pages

Unit 5 CS pt3

The document discusses polymorphism in programming, specifically focusing on operator overloading and method overriding in Python. It explains how operators can be overloaded to perform different tasks based on the data types involved and illustrates method overriding where a subclass can redefine methods from a superclass. Examples are provided to demonstrate these concepts in action, highlighting the flexibility and functionality of object-oriented programming.

Uploaded by

Shreeya Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Unit 5 CS pt3

The document discusses polymorphism in programming, specifically focusing on operator overloading and method overriding in Python. It explains how operators can be overloaded to perform different tasks based on the data types involved and illustrates method overriding where a subclass can redefine methods from a superclass. Examples are provided to demonstrate these concepts in action, highlighting the flexibility and functionality of object-oriented programming.

Uploaded by

Shreeya Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit V: Classes and Objects 2020

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.

Operator Overloading in Polymorphism

Operator Overloading means giving extended meaning beyond their predefined


operational meaning. For example operator + is used to add two integers as well as
join two strings and merge two lists. It is achievable because ‘+’ operator is
overloaded by int class and str class. The same built-in operator or function shows
different behavior for objects of different classes, this is called Operator
Overloading.It is a polymorphic nature of any object oriented programming.

Following table shows gives a list of operators and their respective Python methods
for overloading.

1 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

Example 2: use of + operator for different purposes

>>>print(1 + 2)

Output

>>>print("python"+"program") # concatenate two strings

Output

pythonprogram

Example 3: use of * operator for different purposes

>>>print(3 * 4)

2 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

Output

12

>>>print("python"*4) # repeats the string

Output

python pythonpythonpython

When we use + operator, the magic method __add__ is automatically invoked in


which the operation for + operator is defined. There by changing this magic
method’s code, we can give extra meaning to the + operator.

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.

Example 4: how to overload a binary + operator

class A:

def __init__(self, a):

self.a = a

# adding two objects

3 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

def __add__(self, o):

return self.a + o.a

ob1 = A(1)

ob2 = A(2)

ob3 = A("binary")

ob4 = A("operator")

print(ob1 + ob2)

print(ob3 + ob4)

Output

Binaryoperator

Method Overriding in Polymorphism (Run-time polymorphism)

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.

4 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

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):

print("Base class called")

class B(A):

def foo(self):

print("Child class called")

a1=A()

b1=B()

a1.foo()

b1.foo()

Output:

Base class called

Child class called

5 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

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):

print("explore() method from class A")

class B(A):

def explore(self):

print("explore() method from class B")

b = B()

a = A()

b.explore() # override the baseclass explore()

a.explore()

Output

explore() method from class B

6 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

explore() method from class A

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):

print(self.length*self.breadth," is area of rectangle")

class Square(Rectangle):

def __init__(self,side):

self.side = side

Rectangle.__init__(self,side,side)

def getArea(self):

print(self.side*self.side," is area of square")

s = Square(4)

r = Rectangle(2,4)

7 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

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):

print("Base class called")

class B(A):

def foo(self):

A.foo(self)

8 Department of Computer Science &Engg, PESU


Unit V: Classes and Objects 2020

print("Child class called")

a1=A()

b1=B()

a1.foo()

b1.foo()

Output:

Base class called

Base class called

Child class called

9 Department of Computer Science &Engg, PESU

You might also like