-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance2.py
More file actions
221 lines (130 loc) · 2.83 KB
/
Inheritance2.py
File metadata and controls
221 lines (130 loc) · 2.83 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Example 4
class Animal:
def sound(self):
print("All animals Makes sound")
class Dog(Animal):
def bark(self):
print("Dogs generally barks")
class Cat(Animal):
def meow(self):
print("Cats Meow")
dog1 = Dog()
dog1.sound()
dog1.bark()
cat1 = Cat()
cat1.sound()
cat1.meow()
# 5 Example
#Multiple Inheritance
print("Example 5")
class Father:
def father_skill(self):
print("Good at farming")
class Mother:
def mother_skill(self):
print("Good at cooking")
class Child(Father,Mother):
def child_skill(self):
print("Good footballer")
c = Child()
c.father_skill()
c.mother_skill()
c.child_skill()
# Multilevel Inheritance
print("MultiLevel inheritance Question")
class Grandfather:
def asset(self):
print("Owns Homes")
class Father(Grandfather):
def car(self):
print("Own a car")
class Child(Father):
def laptop(self):
print("Owns a laptop")
Child = Child()
Child.asset()
Child.car()
Child.laptop()
# Questions to practise it out
class Person:
def greet(Self):
print("Hello, I am a person")
class Student(Person):
def study(self):
print("I am studying")
stu = Student()
stu.greet()
stu.study()
# 2 Question MEthod overriding
class Shape:
def area(self):
print("Area is undefined")
class Circle(Shape):
def area(self):
print("Area = 3.14 *r *r")
s1 = Shape()
s1.area()
s2 = Circle()
s2.area()
# 3 Practise Question
class Animal:
def eat(self):
print("I can eat")
class Dog(Animal):
def bark(self):
print("Woof woof!!")
d1 = Dog()
d1.eat()
d1.bark()
# 4 Question practise
class Laptop:
def __init__(self,brand):
self.brand = brand
print(brand)
class GamingLaptop(Laptop):
def __init__(self, brand):
super().__init__(brand)
def features(self):
print("RGB Keyboard and high performance")
c1 = Laptop("Acer")
c2 = GamingLaptop("DEll")
c2.features()
# 5 question pratice
class Father:
def work(self):
print("Works as Engineer")
class Mother:
def hobby(self):
print("Loves Gardening")
class Child(Father , Mother):
def play(self):
print("Playing Football")
c1 = Child()
c1.work()
c1.hobby()
c1.play()
# 6 Practise questions
class Grandfather:
def property(self):
print("Owns Land")
class Father(Grandfather):
def car(self):
print("Owns a car")
class Son(Father):
def bike(self):
print("Owns a bike")
son = Son()
son.property()
son.car()
son.bike()
# 7 Override Method
class Employee:
def work(self):
print("Works 9 to 5")
class Freelancer(Employee):
def work(self):
print("Works Flexible Hours")
employee =Employee()
employee.work()
freelancer =Freelancer()
freelancer.work()