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?
Tesla Model S \n Battery Capacity: 100 kWh
Battery Capacity: 100 kWh \n Tesla Model S
Tesla Model S
Battery Capacity: 100 kWh
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:

Your comments will be displayed after verification.