-
Notifications
You must be signed in to change notification settings - Fork 22
/
polymorphism-1.py
48 lines (35 loc) · 1.19 KB
/
polymorphism-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#polymorphism-1.py
# Polymorphism means having the same interface/attributes in different
# classes.
# Polymorphism is the characteristic of being able to assign
# a different meaning or usage in different contexts.
# A not-so-clear/clean example is, different classes can have
# the same function name.
# Here, the class Dog and Cat has the same method named 'show_affection'
# Even if they are same, both does different actions in the instance.
#
# Since the order of the lookup is
# 'instance' -> 'class' -> 'parent class', even if the
# 'class' and 'parent class' has functions with the same name,
# the instance will only pick up the first hit,
# ie.. from the 'class' and won't go to the parent class.
class Animal(object):
def __init__(self, name):
self.name = name
def eat(self, food):
print("{0} eats {1}".format(self.name, food))
class Dog(Animal):
def show_affection(self):
print("{0} wags tail".format(self.name))
class Cat(Animal):
def show_affection(self):
print("{0} purrs".format(self.name))
for a in (Dog("Tuffy"), Cat("Mona"), Cat("Lucy"), Dog("Tommy")):
a.show_affection()
'''
O/P-
Tuffy wags tail
Mona purrs
Lucy purrs
Tommy wags tail
'''