-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chapter 42 - Inheritance.py
69 lines (52 loc) · 1.36 KB
/
Chapter 42 - Inheritance.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
59
60
61
62
63
64
65
66
67
68
69
# CHAPTER 42
# INHERITANCE - classes can become parents
# and have multiple children
# NOTE: Inheritance allows developers
# to not copy and paste code multiple times because
# of changing multiple classes that
# can be clustered or grouped
# Parent Class
class Animal:
alive = True
def eat(self):
print("This animal is eating")
def sleep(self):
print("This animal is sleeping")
# Subclass
# Rabbit inherits the properties and methods
# of Animal class
# Meaning rabbit can call eat() and sleep()
# from the Animal Cass
class Rabbit(Animal):
def run(self):
print("This rabbit is running")
# Subclass
# Fish inherits the properties and methods
# of Animal class
# Meaning rabbit can call eat() and sleep()
# from the Animal Cass
class Fish(Animal):
def swim(self):
print("This fish is sleeping")
# Subclass
# Hawk inherits the properties and methods
# of Animal class
# Meaning rabbit can call eat() and sleep()
# from the Animal Cass
class Hawk(Animal):
def fly(self):
print("This hawk is flying")
rabbit = Rabbit()
fish = Fish()
hawk = Hawk()
# Calling the properties or method
# from the Parent Class
# Demonstrating the inheritance
# print(rabbit.alive)
# fish.eat()
# hawk.sleep()
# Calling the methods unique
# to each subclass
rabbit.run()
fish.swim()
hawk.fly()