Python Programming - Polymorphism - Discussion

Discussion Forum : Polymorphism - General Questions (Q.No. 57)
57.
What is the output of the following Python code?
class Car:
    def start(self):
        return "Car starting"

class ElectricCar(Car):
    def start(self):
        return "Electric car starting"

def drive(car):
    return car.start()

car = Car()
electric_car = ElectricCar()

print(drive(car))
print(drive(electric_car))
Car starting\nCar starting
Electric car starting\nElectric car starting
Car starting\nElectric car starting
Electric car starting\nCar starting
Answer: Option
Explanation:
The drive() function demonstrates polymorphism, accepting both Car and ElectricCar instances and producing different outputs based on their specific implementations of the start() method.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.