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?
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers