Python Programming - Classes - Discussion

Discussion Forum : Classes - General Questions (Q.No. 37)
37.
Consider the following Python code:
class Vehicle:
    def __init__(self, brand):
        self.brand = brand

    def start_engine(self):
        print(f"The {self.brand} engine is starting")

class ElectricCar(Vehicle):
    def __init__(self, brand, battery_capacity):
        super().__init__(brand)
        self.battery_capacity = battery_capacity

    def start_engine(self):
        print(f"The electric car with {self.battery_capacity} kWh battery is starting")

# Create an instance of the ElectricCar class
my_electric_car = ElectricCar("Tesla", 75)

# Call the start_engine method
my_electric_car.start_engine()
What will be the output of the code?
The electric car with 75 kWh battery is starting
The Tesla engine is starting
The electric car is starting
The Tesla with 75 kWh battery engine is starting
Answer: Option
Explanation:
The code creates an instance of the `ElectricCar` class, which overrides the `start_engine` method. The output reflects the overridden method in the subclass.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.