Vehicle Class Hierarchy

Write a class Vehicle and then define child classes Car, Bike, Truck, each having unique properties like number of wheels and methods to return their type.

Example 1:

Input: Car() 
Output: "This is a car with 4 wheels."

Example 2:

Input: Bike() 
Output: "This is a bike with 2 wheels."

Create properties for the number of wheels in each child class and a method to return a string describing the type of vehicle.

class Vehicle:
    def type(self):
        pass

class Car(Vehicle):
    def type(self):
        return "This is a car with 4 wheels."

class Bike(Vehicle):
    def type(self):
        return "This is a bike with 2 wheels."

class Truck(Vehicle):
    def type(self):
        return "This is a truck with 6 wheels."

# Test the classes
car = Car()
print(car.type())  # Output: This is a car with 4 wheels.

bike = Bike()
print(bike.type())  # Output: This is a bike with 2 wheels.

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!