Skip to content

Commit e6d0da9

Browse files
committed
solution of oops
1 parent b02d7b1 commit e6d0da9

File tree

2 files changed

+133
-19
lines changed

2 files changed

+133
-19
lines changed

Oops/Assignment/Solutions.md

+115
Original file line numberDiff line numberDiff line change
@@ -445,20 +445,135 @@ move_transport(boat) # Output: Boat is sailing
445445

446446
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.
447447
```python
448+
class Animal:
449+
def action(self):
450+
print("Animal is doing something")
451+
452+
class Bird(Animal):
453+
def action(self):
454+
print("Bird is flying")
455+
456+
class Fish(Animal):
457+
def action(self):
458+
print("Fish is swimming")
459+
460+
# Function to demonstrate polymorphism
461+
def perform_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
448472

449473

450474
```
451475

452476
### **Method Overriding and Super()**
453477

454478
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+
class Phone:
481+
def call(self):
482+
print("Making a call")
483+
484+
class Smartphone(Phone):
485+
def call(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+
```
455495

456496
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+
class Vehicle:
499+
def start(self):
500+
print("Vehicle is starting")
501+
502+
class ElectricCar(Vehicle):
503+
def start(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+
457514

458515
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+
class Food:
518+
def display(self):
519+
print("Food Menu")
520+
521+
class Dessert(Food):
522+
def display(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+
```
459532

460533
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.
534+
```python
535+
class Course:
536+
def __init__(self, title, duration):
537+
self.title = title
538+
self.duration = duration
539+
540+
def display_info(self):
541+
print(f"Course Title: {self.title}, Duration: {self.duration} hours")
542+
543+
class OnlineCourse(Course):
544+
def __init__(self, title, duration, platform):
545+
super().__init__(title, duration)
546+
self.platform = platform
547+
548+
def display_info(self):
549+
super().display_info() # Call parent method
550+
print(f"Platform: {self.platform}")
551+
552+
# Create object
553+
online_course = OnlineCourse("Python Programming", 40, "Coursera")
554+
online_course.display_info()
555+
# Output: Course Title: Python Programming, Duration: 40hours
556+
# Platform: Coursera
557+
558+
```
559+
461560

462561
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.
562+
```python
563+
class Employee:
564+
def work(self):
565+
print("Employee is working")
566+
567+
class Manager(Employee):
568+
def work(self):
569+
super().work() # Call parent method
570+
print("Manager is overseeing work")
463571

572+
# Create object
573+
manager = Manager()
574+
manager.work()
575+
# Output: Employee is working
576+
# Manager is overseeing work
577+
578+
```
464579
---

Oops/Practice.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
# Define base class Animal
2-
class Animal:
3-
# Method that will be overridden in subclasses
4-
def make_sound(self):
5-
print("Animal makes a sound")
1+
class Payment:
2+
def process_payment(self):
3+
pass
64

7-
# Define the subclass Dog that inherits from Animal
8-
class Dog(Animal):
9-
# Override the make_sound method for Dog
10-
def make_sound(self):
11-
print("Bark")
5+
class CreditCardPayment(Payment):
6+
def process_payment(self):
7+
print("Processing credit card payment")
128

13-
# Define the subclass Cat that inherits from Animal
14-
class Cat(Animal):
15-
# Override the make_sound method for Cat
16-
def make_sound(self):
17-
print("Meow")
9+
class PayPalPayment(Payment):
10+
def process_payment(self):
11+
print("Processing PayPal payment")
1812

19-
dog = Dog()
20-
cat = Cat()
13+
# Test polymorphism
14+
def process(payment):
15+
payment.process_payment()
2116

22-
dog.make_sound() # Output: Bark
23-
cat.make_sound() # Output: Meow
17+
credit_card = CreditCardPayment()
18+
paypal = PayPalPayment()
19+
20+
process(credit_card)
21+
# Output: Processing credit card payment
22+
process(paypal)

0 commit comments

Comments
 (0)