-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathinheritance-2.py
58 lines (41 loc) · 1.28 KB
/
inheritance-2.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
49
50
51
52
53
54
55
56
57
58
# inheritance-2.py
# The code below shows another example of inheritance
# Dog and Cat are two classes which inherits from Animal.
# This an instance created from Dog or Cat can access the methods
# in the Animal class, ie.. eat().
# The instance of 'Dog' can access the methods of the Dog class
# and it's parent class 'Animal'.
# The instance of 'Cat' can access the methods of the Cat class
# and it's parent class 'Animal'.
# But the instance created from 'Cat' cannot access the attributes
# within the 'Dog' class, and vice versa.
class Animal(object):
def __init__(self, name):
self.name = name
def eat(self, food):
print("%s is eating/drinking %s" % (self.name, food))
class Dog(Animal):
def fetch(self, thing):
print("%s goes after the %s" % (self.name, thing))
class Cat(Animal):
def swatstring(self):
print("%s shred the string!" % self.name)
dog = Dog("Tuffy")
cat = Cat("Mona")
dog.fetch("paper")
dog.eat("bone")
print("--------")
cat.eat("milk")
cat.swatstring()
'''
O/P-
Tuffy goes after the paper
Tuffy is eating/drinking bone
--------
Mona is eating/drinking milk
Mona shred the string!
'''
# The below methods would fail, since the instances doesn't have
# have access to the other class.
cat.fetch("frizbee")
dog.swatstring()