Vehicle Speeds with Polymorphism

Create a class Vehicle with a method max_speed(). Extend this class into Car and Bike, each implementing the max_speed() method to return their maximum speeds.

Example 1:

Input: Car 
Output: "Car's max speed is 200 mph"

Example 2:

Input: Bike
Output: "Bike's max speed is 30 mph"

Override the max_speed() method in each subclass to return the respective maximum speed of the vehicle.

class Vehicle:
    def max_speed(self):
        pass

class Car(Vehicle):
    def max_speed(self):
        return "Car's max speed is 200 mph"

class Bike(Vehicle):
    def max_speed(self):
        return "Bike's max speed is 30 mph"

# Test the classes
car = Car()
print(car.max_speed())  # Output: Car's max speed is 200 mph

bike = Bike()
print(bike.max_speed())  # Output: Bike's max speed is 30 mph

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!