You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Oops/Assignment/Solutions.md
+115
Original file line number
Diff line number
Diff line change
@@ -445,20 +445,135 @@ move_transport(boat) # Output: Boat is sailing
445
445
446
446
15.**Animal Actions**: Write a base class `Animal` with a method `action()`. Create subclasses `Bird` and `Fish` that override the `action()` method (e.g., birds fly and fish swim). Write a function that takes an `Animal` object and calls its `action()` method, demonstrating polymorphism.
447
447
```python
448
+
classAnimal:
449
+
defaction(self):
450
+
print("Animal is doing something")
451
+
452
+
classBird(Animal):
453
+
defaction(self):
454
+
print("Bird is flying")
455
+
456
+
classFish(Animal):
457
+
defaction(self):
458
+
print("Fish is swimming")
459
+
460
+
# Function to demonstrate polymorphism
461
+
defperform_action(animal):
462
+
animal.action()
463
+
464
+
# Create objects
465
+
bird = Bird()
466
+
fish = Fish()
467
+
468
+
perform_action(bird)
469
+
# Output: Bird is flying
470
+
perform_action(fish)
471
+
# Output: Fish is swimming
448
472
449
473
450
474
```
451
475
452
476
### **Method Overriding and Super()**
453
477
454
478
16.**Smartphone Class**: Create a base class `Phone` with a method `call()` that prints "Making a call." Create a subclass `Smartphone` that overrides the `call()` method to also print "Using an app to make a call." Use the `super()` function to call the parent class method from the subclass.
479
+
```python
480
+
classPhone:
481
+
defcall(self):
482
+
print("Making a call")
483
+
484
+
classSmartphone(Phone):
485
+
defcall(self):
486
+
super().call() # Call parent method
487
+
print("Using an app to make a call")
488
+
489
+
# Create object
490
+
smartphone = Smartphone()
491
+
smartphone.call() # Output: Making a call
492
+
# Using an app to make a call
493
+
494
+
```
455
495
456
496
17.**Vehicle Overriding**: Create a base class `Vehicle` with a method `start()` that prints "Vehicle is starting." Create a subclass `ElectricCar` that overrides `start()` to print "Electric car is starting." Use the `super()` method to call the `Vehicle`'s `start()` method before adding its own functionality.
497
+
```python
498
+
classVehicle:
499
+
defstart(self):
500
+
print("Vehicle is starting")
501
+
502
+
classElectricCar(Vehicle):
503
+
defstart(self):
504
+
super().start() # Call parent method
505
+
print("Electric car is starting")
506
+
507
+
# Create object
508
+
electric_car = ElectricCar()
509
+
electric_car.start() # Output: Vehicle is starting
510
+
# Electric car is starting
511
+
512
+
```
513
+
457
514
458
515
18.**Food Menu**: Write a base class `Food` with a method `display()` that prints "Food Menu". Create a subclass `Dessert` that overrides `display()` to print "Dessert Menu" but also calls the parent class's `display()` using `super()`.
516
+
```python
517
+
classFood:
518
+
defdisplay(self):
519
+
print("Food Menu")
520
+
521
+
classDessert(Food):
522
+
defdisplay(self):
523
+
super().display() # Call parent method
524
+
print("Dessert Menu")
525
+
526
+
# Create object
527
+
dessert = Dessert()
528
+
dessert.display() # Output: Food Menu
529
+
# Dessert Menu
530
+
531
+
```
459
532
460
533
19.**Online Course**: Create a base class `Course` with attributes `title` and `duration` and a method `display_info()` that prints course details. Create a subclass `OnlineCourse` that adds an attribute `platform` and overrides `display_info()` to include the platform name. Use `super()` to call the parent class’s method and extend it.
20.**Company Hierarchy**: Create a base class `Employee` with a method `work()`. Create a subclass `Manager` that overrides the `work()` method but calls the parent class’s `work()` using `super()` to print both the employee’s and manager’s work duties.
0 commit comments