@@ -157,4 +157,68 @@ def __init__(self,name,breed,age):
157
157
def __sound (self ):
158
158
print ("Woof" )
159
159
def __eat (self ,meal ):
160
- print (f"{ self .__name } is eating { meal } " )
160
+ print (f"{ self .__name } is eating { meal } " )
161
+
162
+ c1 = Dog ("Bella" ,"germen" ,"4" )
163
+ c1 .__name = "Bella" # This will raise an AttributeError
164
+ # c1.__sound() # This will raise an AttributeError
165
+ # c1.__eat("Biscuit") # This will raise an AttributeError
166
+
167
+ #how to access the private attributes
168
+ c1 ._Dog__name = "Bella"
169
+ c1 ._Dog__sound ()
170
+ c1 ._Dog__eat ("Biscuit" )
171
+ print (c1 ._Dog__name )
172
+ print (c1 ._Dog__breed )
173
+ print (c1 ._Dog__age ) # This will print 0 because we didn't pass
174
+
175
+
176
+ # POLYMORPHISM
177
+ class Animal :
178
+ def sound (self ):
179
+ pass
180
+ class Dog (Animal ):
181
+ def sound (self ):
182
+ return "Woof"
183
+ class Cat (Animal ):
184
+ def sound (self ):
185
+ return "Meow"
186
+
187
+
188
+ c2 = Cat ()
189
+ c3 = Dog ()
190
+ print (c2 .sound ()) # Output: Meow
191
+ print (c3 .sound ()) # Output: Woof
192
+
193
+ #ABSTRACTION
194
+ from abc import ABC , abstractmethod
195
+ class Shape :
196
+ @abstractmethod
197
+ def area (self ):
198
+ pass
199
+ @abstractmethod
200
+ def perimeter (self ):
201
+ pass
202
+ class Circle :
203
+ def __init__ (self ,r ):
204
+ self .radius = r
205
+ def area (self ):
206
+ return 3.14 * (self .radius ** 2 )
207
+ def perimeter (self ):
208
+ return 2 * 3.14 * self .radius
209
+ class Rectangle :
210
+ def __init__ (self ,l ,w ):
211
+ self .length = l
212
+ self .width = w
213
+ def area (self ):
214
+ return self .length * self .width
215
+ def perimeter (self ):
216
+ return 2 * (self .length + self .width )
217
+
218
+ c = Circle (5 )
219
+ r = Rectangle (4 ,5 )
220
+ print (c .area ())
221
+ print (c .perimeter ())
222
+ print (r .area ())
223
+ print (r .perimeter ())
224
+
0 commit comments