0% found this document useful (0 votes)
26 views7 pages

Lecture 23 Polymorphism

The document discusses the concept of polymorphism in object-oriented programming. It provides examples of polymorphism using classes for cars and boats that have shared methods like go(), turnLeft(), and turnRight() but different implementations. It then creates lists of car and boat objects and calls their methods to demonstrate polymorphism in action.

Uploaded by

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

Lecture 23 Polymorphism

The document discusses the concept of polymorphism in object-oriented programming. It provides examples of polymorphism using classes for cars and boats that have shared methods like go(), turnLeft(), and turnRight() but different implementations. It then creates lists of car and boat objects and calls their methods to demonstrate polymorphism in action.

Uploaded by

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

Polymorphism

Polymorphism
• World Polymorphism comes form
• Poly + morph

• When objects behave as per their form/identity, the concept is called


polymorphism
Car
class Car:
def __init__(self, id):
self.no = id
def go(self):
print("Car no: ",self.no, "drives away")
def turnLeft(self):
print("Car no:",self.no, "turning lelft")
def turnRight(self):
print("Car no:",self.no, "turning right")
Boat
class Boat:
def __init__(self, id):
self.no = id
def go(self):
print("Boat no: ",self.no, "sails away")
def turnLeft(self):
print("Boat no:",self.no,"sailing lelft")
def turnRight(self):
print("Boat no:",self.no,"sailing right")
c = Car(5)
d = Boat(7)

c.go()
c.turnLeft()
c.turnRight()

d.go()
d.turnLeft()
d.turnRight()
things=[]
for i in range(0,200):
if i%2==0:
things.append(Car(i))
if i%2!=0:
things.append(Boat(100+i))

for a in range(0,200):
things[a].go()
Permanent Employee

Example 2 Salary
Allowance

Employee
Get Gross Pay
Get Tax
Salary Get Net Pay
Allowance
Contract Employee

Get Gross Pay Salary


Allowance

Get Gross Pay


Get Tax
Get Net Pay

You might also like