- Created a Base class 'Vehicle' having attributes make, model, and year
class Vehicle:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start(self):
print("Starting the vehicle.")
def stop(self):
print("Stopping the vehicle.")
def accelerate(self):
print("Accelerating the vehicle.")
def brake(self):
print("Applying brakes.")
- Now Created a Sub-class named ElectricVehicle having attribute battery_capacity
class ElectricVehicle(Vehicle):
def __init__(self, make, model, year, battery_capacity):
super().__init__(make, model, year)
self.battery_capacity = battery_capacity
def charge(self):
print("Charging the battery.")
def check_battery(self):
print("Checking the battery level.")
- Now Created a Sub-class named HybridVehicle having attribute fuel_capacity
class HybridVehicle(Vehicle):
def __init__(self, make, model, year, fuel_capacity):
super().__init__(make, model, year)
self.fuel_capacity = fuel_capacity
def refuel(self):
print("Refueling the vehicle.")
def check_fuel(self):
print("Checking the fuel level.")
- Finally Created class HybridElectricVehicle and inherited ElectricVehicle and HybridVehicle
class HybridElectricVehicle(ElectricVehicle, HybridVehicle):
def __init__(self, make, model, year, battery_capacity, fuel_capacity, additional_property):
super().__init__(make, model, year, battery_capacity)
HybridVehicle.__init__(self, make, model, year, fuel_capacity)
self.additional_property = additional_property
Creating an instance of HybridElectricVehicle
hybrid_electric_car = HybridElectricVehicle("Tesla", "Model 3", 2023, 75, 10, 'additional_property')
`
In this code i'm facing this error:
Traceback (most recent call last):
File "multiple inheritance 2.py", line 52, in <module>
hybrid_electric_car = HybridElectricVehicle("Tesla", "Model 3", 2023, 75, 10, 'additional_property')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "multiple inheritance 2.py", line 46, in __init__
super().__init__(make, model, year, battery_capacity)
File multiple inheritance 2.py", line 22, in __init__
super().__init__(make, model, year)
TypeError: HybridVehicle.__init__() missing 1 required positional argument: 'fuel_capacity'