class Vehicle:
"""Base class for all vehicles."""
def __init__(self, make: str, model: str, year: int):
self.make = make
self.model = model
self.year = year
self.mileage = 0
def start_engine(self):
"""Start the vehicle's engine."""
print(f"The {self.year} {self.make} {self.model} engine starts.")
def drive(self, miles: float):
"""Drive the vehicle and update mileage."""
self.mileage += miles
print(f"Drove {miles} miles. Total mileage: {self.mileage}")
class ElectricCar(Vehicle):
"""Electric vehicle with battery management."""
def __init__(self, make: str, model: str, year: int, battery_capacity: float):
super().__init__(make, model, year) # Call parent constructor
self.battery_capacity = battery_capacity
self.battery_level = 100.0 # Start fully charged
def start_engine(self):
"""Override: Electric cars don't have traditional engines."""
print(f"The {self.year} {self.make} {self.model} powers on silently.")
def charge(self, hours: float):
"""Charge the battery (unique to electric cars)."""
charge_added = min(hours * 10, 100 - self.battery_level)
self.battery_level += charge_added
print(f"Charged for {hours} hours. Battery: {self.battery_level:.1f}%")
# Inheritance in action
tesla = ElectricCar("Tesla", "Model S", 2023, 100.0)
tesla.start_engine() # Uses overridden method
tesla.drive(50) # Uses inherited method
tesla.charge(2) # Uses unique method