Created
May 13, 2025 17:36
-
-
Save abraham-ny/6feaf0ed0e60fbe901f86ac1894be2db to your computer and use it in GitHub Desktop.
Polymorphysm assignment PLP Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SmartDevice: | |
| def __init__(self, brand, model, battery): | |
| """ | |
| Initialize the SmartDevice with common attributes. | |
| """ | |
| self.brand = brand | |
| self.model = model | |
| self.battery = battery | |
| def charge(self): | |
| """ | |
| Method to charge the device. | |
| """ | |
| print(f"Charging {self.brand} {self.model}... π") | |
| self.battery = 100 | |
| print(f"{self.brand} {self.model} is now fully charged!") | |
| # Child Class: Smartphone (Inherits from SmartDevice) | |
| class Smartphone(SmartDevice): | |
| def __init__(self, brand, model, storage, battery): | |
| """ | |
| Initialize the Smartphone with specific attributes. | |
| """ | |
| super().__init__(brand, model, battery) | |
| self.storage = storage | |
| def make_call(self, contact): | |
| """ | |
| Method to make a call. | |
| """ | |
| if self.battery > 0: | |
| print(f"π Calling {contact} from {self.brand} {self.model}...") | |
| else: | |
| print(f"Battery is dead! Please charge your {self.brand} {self.model}.") | |
| def send_message(self, contact, message): | |
| """ | |
| Method to send a message. | |
| """ | |
| if self.battery > 0: | |
| print(f"βοΈ Sending message to {contact}: '{message}'") | |
| else: | |
| print(f"Battery is dead! Cannot send messages.") | |
| def check_storage(self): | |
| """ | |
| Method to display available storage. | |
| """ | |
| print(f"{self.brand} {self.model} has {self.storage}GB of storage available.") | |
| # Child Class: Smartwatch (Inherits from SmartDevice) | |
| class Smartwatch(SmartDevice): | |
| def __init__(self, brand, model, battery, health_tracker): | |
| """ | |
| Initialize the Smartwatch with specific attributes. | |
| """ | |
| super().__init__(brand, model, battery) | |
| self.health_tracker = health_tracker | |
| def show_health_data(self): | |
| """ | |
| Method to display health tracking data. | |
| """ | |
| print(f"{self.brand} {self.model} Health Data: {self.health_tracker}") | |
| #usage | |
| phone = Smartphone("Samsung", "Galaxy S21", 128, 50) | |
| phone.make_call("Alice") | |
| phone.send_message("Bob", "Hello, how are you?") | |
| phone.check_storage() | |
| phone.charge() | |
| watch = Smartwatch("Apple", "Watch Series 7", 60, {"Heart Rate": "72 bpm", "Steps": "5,000"}) | |
| watch.show_health_data() | |
| watch.charge() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Vehicle: | |
| def move(self): | |
| """ | |
| Method to be overridden by child classes. | |
| """ | |
| raise NotImplementedError("Subclasses must implement this method.") | |
| # Child Class: Car (Inherits from Vehicle) | |
| class Car(Vehicle): | |
| def move(self): | |
| """ | |
| Overrides the move method to represent car movement. | |
| """ | |
| print("π Driving on the road...") | |
| # Child Class: Plane (Inherits from Vehicle) | |
| class Plane(Vehicle): | |
| def move(self): | |
| """ | |
| Overrides the move method to represent plane movement. | |
| """ | |
| print("βοΈ Flying in the sky...") | |
| # how to use | |
| vehicles = [Car(), Plane()] | |
| for vehicle in vehicles: | |
| vehicle.move() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment