-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38_Abstract class and Abstract method.py
More file actions
284 lines (180 loc) · 4.44 KB
/
Copy path38_Abstract class and Abstract method.py
File metadata and controls
284 lines (180 loc) · 4.44 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Abstract Class and Abstract Method :
# Abstract Class :
# a)
from abc import ABC, abstractmethod
class Shape(ABC) :
@abstractmethod
def area(self) :
pass
class Circle(Shape) :
def area(self,r) :
print(f'area of circle : {3.14*r*r}')
class Square(Shape) :
def area(self,s) :
print(f'area of square : {s*s}')
c = Circle()
c.area(6)
s = Square()
s.area(5)
print('-'*20)
# b)
class Vehicle(ABC) :
@abstractmethod
def no_of_seats(self) :
pass
@abstractmethod
def no_of_wheels(self) :
pass
class Car(Vehicle) :
def no_of_seats(self):
print('Number of seats of a Car : 5')
def no_of_wheels(self):
print('Number of wheels of a Car : 4')
class Bus(Vehicle) :
def no_of_seats(self):
print('Number of seats of a Bus : 30')
def no_of_wheels(self):
print('Number of wheels of a Bus : 4')
class Auto(Vehicle) :
def no_of_seats(self):
print('Number of seats of a Auto : 2')
def no_of_wheels(self):
print('Number of wheels of a Auto : 3')
a = Auto()
a.no_of_seats()
a.no_of_wheels()
b = Bus()
b.no_of_seats()
b.no_of_wheels()
c = Car()
c.no_of_seats()
c.no_of_wheels()
print('-'*20)
# c) Customize :
class Vehicle(ABC) :
@abstractmethod
def no_of_seats(self) :
pass
@abstractmethod
def no_of_wheels(self) :
pass
class Car(Vehicle) :
def no_of_seats(self, seats):
print(f'Number of seats of a Car : {seats}')
def no_of_wheels(self, wheels):
print(f'Number of wheels of a Car : {wheels}')
class Bus(Vehicle) :
def no_of_seats(self, seats):
print(f'Number of seats of a Bus : {seats}')
def no_of_wheels(self, wheels):
print(f'Number of wheels of a Bus : {wheels}')
class Auto(Vehicle) :
def no_of_seats(self, seats):
print(f'Number of seats of a Auto : {seats}')
def no_of_wheels(self, wheels):
print(f'Number of wheels of a Auto : {wheels}')
a = Auto()
a.no_of_seats(2)
a.no_of_wheels(3)
b = Bus()
b.no_of_seats(20)
b.no_of_wheels(6)
c = Car()
c.no_of_seats(4)
c.no_of_wheels(4)
print('-'*20)
# d) abstract class contain abstract method as well as concrete method :
class Car(ABC) :
@abstractmethod
def no_of_seats(self) :
pass
def break_type(self) :
print('Normal Break')
class Honda(Car) :
def no_of_seats(self):
print('Number of seats : 6')
class Audi(Car) :
def no_of_seats(self):
print('Number of seats : 4')
def break_type(self):
print('Special Break')
class SUV(Car) :
def no_of_seats(self):
print('Number of seats : 4')
def break_type(self):
print('Special with powerful Break')
class Coupe(Car) :
def no_of_seats(self):
print('Number of seats : 5')
h = Honda()
h.no_of_seats()
h.break_type()
a = Audi()
a.no_of_seats()
a.break_type()
s = SUV()
s.no_of_seats()
s.break_type()
c = Coupe()
c.no_of_seats()
c.break_type()
print('-'*20)
# e) Concrete class VS Abstract class :
# Example 1 :
class Test():
def method1(self) :
pass
t = Test()
t.method1()
# Example 2 :
from abc import ABC, abstractmethod
class Test(ABC) :
@abstractmethod
def ab1(self) :
pass
# t = Test() #TypeError: Can't instantiate abstract class Test with abstract method ab1
# Example 3 :
class Test(ABC) :
def method1(self) :
print('Hello!')
t = Test()
# Example 4 :
class Test(ABC) :
def method1(self) :
print('Hello!')
@abstractmethod
def ab1(self) :
pass
class Child(Test) :
def ab1(self) :
print('Hii')
c = Child()
c.ab1()
c.method1()
print('-'*20)
# Example 5 :
class Test(ABC) :
def method1(self) :
print('Hello!')
@abstractmethod
def ab1(self) :
pass
class Child(Test) :
def ab1(self) :
pass
c = Child()
c.ab1()
c.method1()
print('-'*20)
# Example 6 :
class Test(ABC) :
def method1(self) :
print('Hello!')
@abstractmethod
def ab1(self) :
pass
class Child(Test) :
pass
# c = Child() #TypeError: Can't instantiate abstract class Child with abstract method ab1
c.ab1()
c.method1()