-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritace.py
More file actions
336 lines (257 loc) · 9.77 KB
/
Inheritace.py
File metadata and controls
336 lines (257 loc) · 9.77 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
class Parent:
def __init__(self, name):
self.name = name
print("Parent class initialized with name:", self.name)
class Child1(Parent):
def __init__(self, name, age):
super().__init__(name)
self.age = age
print("Name:", self.name)
print("Age:", self.age)
print("Child1 class initialized")
class Child2(Parent):
def __init__(self, name):
super().__init__(name)
print("Child2 class initialized")
c1 = Child1("John", 10)
c2 = Child2("Doe")
class Demo1:
def disp1(self): # Renamed method to avoid conflict with class name
print("Demo1 class initialized")
class Demo2:
def disp2(self): # Fixed inheritance issue MRO - Method Resolution Order
print("Demo2 class initialized")
class Demo3(Demo1, Demo2): # Corrected inheritance
def disp3(self):
print("Demo3 class initialized")
# Create an instance of Demo3
d3 = Demo3()
d3.disp3() # Call disp3 from Demo3
d3.disp2() # Call disp2 from Demo2
d3.disp1() # Call disp1 from Demo1
# Print the Method Resolution Order (MRO) of the Demo3 class
print(Demo3.mro())
# operator # overloading
class Stuent:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name
def __add__(self, other):
if isinstance(other, Stuent):
return self.age + other.age
return NotImplemented
S = Stuent("Anu", 20)
s1 = Stuent("Ram", 21)
print(S.name) # This will print the name of the student
print(S) # This will call the __str__ method and print the name
print(S + s1) # This will add the ages of the two students
print(s1)
print(S == s1) # This will compare the object references, not the names
print(S is s1) # This will check if both variables point to the same object
# Operator overloading for circle class
# This class calculates the area and circumference of a circle based on its radius.
class circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
def circumference(self):
return 2 * 3.14 * self.radius
c = circle(5)
print("Area of circle:", c.area())
print("Circumference of circle:", c.circumference())
c1 = circle(10)
print("Area of circle:", c1.area())
print("Circumference of circle:", c1.circumference())
c2 = circle(15)
print("Area of circle:", c2.area())
print("Circumference of circle:", c2.circumference())
# sum of two rADius
def sum_of_radii(circle1, circle2):
return circle1.radius + circle2.radius
print("Sum of radii:", sum_of_radii(c, c1))
print("Sum of radii:", sum_of_radii(c1, c2))
# Inheritance Example with Software Development Roles
class SoftwareDeveloper:
def __init__(self, name, programming_language):
self.name = name
self.id = id(self)
self.intro = (self.name, self.id, programming_language)
class SoftwareEngineer(SoftwareDeveloper):
def __call__(self):
return f"{self.name} is coding in {self.intro[2]}"
class SoftwareTester(SoftwareDeveloper):
def __call__(self):
return f"{self.name} is testing the software in {self.intro[2]}"
s1 = SoftwareEngineer("John", "Java")
s2 = SoftwareTester("Bob", "Java")
print(s1.intro)
print(s1())
print(s2.intro)
print(s2())
# Single Inheritance Example with Vehicle
class Vehicle:
def start(self):
print("The vehicle is starting.")
# Subclass
class Car(Vehicle):
def honk(self):
print("The car is honking. Beep beep!")
# Creating Instance
car1 = Car()
car1.start() # Output: The vehicle is starting.
car1.honk() # Output: The car is honking. Beep beep!
# Multiple Inheritance Example with Hybrid Car
class Electric:
def charge(self):
print("The vehicle is charging.")
class Engine:
def fuel(self):
print("The vehicle is refueling.")
class HybridCar(Electric, Engine):
def drive(self):
print("The hybrid car is driving.")
# Creating Instance
hybrid_car = HybridCar()
hybrid_car.charge() # Output: The vehicle is charging.
hybrid_car.fuel() # Output: The vehicle is refueling.
# Abstraction easy Example using friuts
from abc import ABC, abstractmethod
class Fruit(ABC):
@abstractmethod
def taste(self):
pass
class Apple(Fruit):
def taste(self):
return "Sweet"
class Orange(Fruit):
def taste(self):
return "Citrus"
class Banana(Fruit):
def taste(self):
return "Sweet and soft"
# Creating instances of each fruit
apple = Apple()
orange = Orange()
banana = Banana()
# Printing the taste of each fruit
print("Apple taste:", apple.taste())
print("Orange taste:", orange.taste())
print("Banana taste:", banana.taste())
# animal example that shows both inheritance and polymorphism
class Animal:
def sound(self):
return "Some generic animal sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
class money(Animal):
def sound(self, sound, tail, dance):
return f"{sound} with {tail} tail and {dance} dance"
def chirp(self):
return "Chirp"
# Creating instances of Dog and Cat
dog = Dog()
cat = Cat()
# Creating an instance of money
mon = money()
# Printing the sounds made by each animal
print("Dog sound:", dog.sound()) # Output: Dog sound: Bark
print("Cat sound:", cat.sound()) # Output: Cat sound: Meow
print("Money sound:", mon.sound("Chirp", "long", "happy")) # Output: Money sound: Chirp with long tail and happy dance
# animal example that shows both inheritance and polymorphism with only monkey with all methods and properties of monkey
class Stuent:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name
def __add__(self, other):
if isinstance(other, Stuent):
return self.age + other.age
return NotImplemented
def __eq__(self, other):
if isinstance(other, Stuent):
return self.name == other.name
return NotImplemented
def __ne__(self, other):
if isinstance(other, Stuent):
return self.name != other.name
return NotImplemented
def __lt__(self, other):
if isinstance(other, Stuent):
return self.age < other.age
return NotImplemented
def __le__(self, other):
if isinstance(other, Stuent):
return self.age <= other.age
return NotImplemented
def __gt__(self, other):
if isinstance(other, Stuent):
return self.age > other.age
return NotImplemented
def __ge__(self, other):
if isinstance(other, Stuent):
return self.age >= other.age
return NotImplemented
def __call__(self):
return f"{self.name} is {self.age} years old"
class money(Stuent):
def __init__(self, name, age, sound, tail, dance):
super().__init__(name, age)
self.sound = sound
self.tail = tail
self.dance = dance
def __call__(self):
return f"{self.name} is {self.age} years old and makes a {self.sound} sound with a {self.tail} tail and does a {self.dance} dance"
# Creating instances of Stuent and money
m = money("Monkey", 5, "Chirp", "long", "happy")
m1 = money("Monkey1", 6, "Chirp", "short", "sad")
m2 = money("Monkey2", 7, "Chirp", "medium", "excited")
m3 = money("Monkey3", 8, "Chirp", "long", "joyful")
m4 = money("Monkey4", 9, "Chirp", "short", "playful")
# Printing the details of each monkey
print(m()) # Output: Monkey is 5 years old and makes a Chirp sound with a long tail and does a happy dance
print(m1()) # Output: Monkey1 is 6 years old and makes a Chir
#p sound with a short tail and does a sad dance
print(m2()) # Output: Monkey2 is 7 years old and makes a Chirp sound with a medium tail and does an excited dance
print(m3()) # Output: Monkey3 is 8 years old and makes a Chir
# Absrtaction example for making payments usaing upi and use names of persons Anu, Ram,
class Payment(ABC):
@abstractmethod
def pay(self):
pass
class UPI(Payment):
def __init__(self, name):
self.name = name
def pay(self):
return f"{self.name} is making a payment using UPI."
class CreditCard(Payment):
def __init__(self, name):
self.name = name
def pay(self):
return f"{self.name} is making a payment using Credit Card."
class DebitCard(Payment):
def __init__(self, name):
self.name = name
def pay(self):
return f"{self.name} is making a payment using Debit Card."
# Creating instances of each payment method
upi_payment = UPI("Anu")
credit_card_payment = CreditCard("Ram")
debit_card_payment = DebitCard("Ram")
# Printing the payment methods
print(upi_payment.pay()) # Output: Anu is making a payment using UPI
print(credit_card_payment.pay()) # Output: Ram is making a payment using Credit Card
print(debit_card_payment.pay()) # Output: Sita is making a payment using Debit Card
# telling users about payment operation is successful
def payment_success(payment_method):
print(f"Payment operation using {payment_method.name} is successful.")
payment_success(upi_payment) # Output: Payment operation using Anu is successful.
payment_success(credit_card_payment) # Output: Payment operation using Ram is successful.
payment_success(debit_card_payment) # Output: Payment operation using Ram is successful.