Transport System with Polymorphism

Implement a class hierarchy for a transportation system. Define a base class Transport and subclasses like Car, Bike, and Bus, each implementing methods for capacity and speed.

Example 1:

Input: Car 
Output: "Car capacity: 5, speed: 120 km/h"

Example 2:

Input: Bike
Output: "Bike capacity: 1, speed: 30 km/h"

Override the capacity() and speed() methods in each subclass to return the capacity and speed of the transport.

class Transport:
    def capacity(self):
        pass

    def speed(self):
        pass

class Car(Transport):
    def capacity(self):
        return 5

    def speed(self):
        return "120 km/h"

class Bike(Transport):
    def capacity(self):
        return 1

    def speed(self):
        return "30 km/h"

class Bus(Transport):
    def capacity(self):
        return 30

    def speed(self):
        return "80 km/h"

# Test the classes
car = Car()
print(f"Car capacity: {car.capacity()}, speed: {car.speed()}")  # Output: Car capacity: 5, speed: 120 km/h

bike = Bike()
print(f"Bike capacity: {bike.capacity()}, speed: {bike.speed()}")  # Output: Bike capacity: 1, speed: 30 km/h

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!