-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadv_inheritance.py
More file actions
183 lines (114 loc) · 3.47 KB
/
adv_inheritance.py
File metadata and controls
183 lines (114 loc) · 3.47 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Single or Basic Inheritance
# Base Class
class Parent:
def __init__(self,name):
self.name = name
def greet(self):
print(f"Hello, {self.name}!")
# Derived Class(Child Class)
class Child(Parent):
def play(self):
print(f"{self.name} is playing!")
# Create an Instance Of Child Class
child = Child("Aniket")
child.greet() # Output: Hello, Aniket!
child.play() # Output: Aniket is playing!
#-------------------------------------------------------------------------------------------------------------------------------
# Multiple Inheritance:
# Base Class:
class Grandparent:
def __init__(self,name):
self.name = name
def tell_story(self):
print(f"{self.name} tells a Story!")
# Intermidiate Class:
class Parent(Grandparent):
def work(self):
print(f"{self.name} is working!")
# Derived Class(Child Class):
class Child(Parent):
def play(self):
print(f"{self.name} is playing!")
# Create an Instance Of Child Class:
child = Child("Aniket")
child.tell_story()
child.work()
child.play()
#-------------------------------------------------------------------------------------------------------------------------
# Hierarchical Inheritance
# Base Class:
class Parent:
def __init__(self,name):
self.name = name
def greet(self):
print(f"Hello, My Name is {self.name}")
# Derived Class 1:
class Child1(Parent):
def play(self):
print(f"{self.name} is Playing")
# Derived Class 2:
class Child2(Parent):
def study(self):
print(f"{self.name} is Studying")
# Create Instance Of child 1 and Child 2 :
child_1 = Child1("Ram")
child_2 = Child2("Shyam")
child_1.greet()
child_1.play()
child_2.greet()
child_2.play()
#---------------------------------------------------------------------------------------------------------------------------
# Multiple Inheritance (Diamond Problem)
# Common Base Class:
class A:
def __init__(self,name):
self.name = name
def greet(self):
print(f"Hello From A, {self.name}.")
# Intermidiate Class 1:
class B(A):
def greet(self):
print(f"Hello From B, {self.name}.")
super().greet()
# Intermidiate Class 2:
class C(A):
def greet(self):
print(f"Hello From C, {self.name}.")
super().greet()
# Derived Class :
class D(B,C):
def greet(self):
print(f"Hello From D, {self.name}.")
super().greet()
# Create an Instance Of D:
d = D("Frank")
d.greet()
#-----------------------------------------------------------------------------------------------------------------------------
# Hybrid Inheritance:
# Base Class:
class Creature:
def __init__(self,name):
self.name = name
def sound(self):
print(f"{self.name} makes a Sound.")
# Intermidiate Class 1 (Hirarchical)
class Mamal(Creature):
def feed(self):
print(f"{self.name} is Feeding Milk")
# Intermidiate Class 2 (Multiple)
class Bird(Creature):
def fly(self):
print(f"{self.name} is Flying")
# Derived Class (Multiple Inheritance)
class Bat(Mamal, Bird):
def __init__(self, name):
Mamal().__init__(name) # Explicitly Calling the Constructor
def nocturnal(self):
print(f"{self.name} is nocturnal")
# Create an Instance of Bat:
bat = Bat("Bruce")
bat.sound()
bat.feed()
bat.fly()
bat.nocturnal()
#------------------------------------------------------------------------------------------------------------------------------------