Lecture 23 Polymorphism

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 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