Python Programming - Classes - Discussion
Discussion Forum : Classes - General Questions (Q.No. 33)
33.
Consider the following Python code:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"{self.brand} {self.model}")
class ElectricCar(Car):
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity
def display_info(self):
super().display_info()
print(f"Battery Capacity: {self.battery_capacity} kWh")
# Create an instance of the ElectricCar class
my_electric_car = ElectricCar("Tesla", "Model S", 100)
# Call the display_info method
my_electric_car.display_info()
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `ElectricCar` class, which overrides the `display_info` method. The output includes messages from both the superclass (`Car`) and the subclass (`ElectricCar`).
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