Editor

Create a Vehicle Subclass

Beginner · Write the code · from Python Inheritance: Subclasses, Polymorphism, and super()

From the lesson: Python Inheritance: Subclasses, Polymorphism, and super()

Think about how scientists classify animals. A Dog is a type of Mammal, which is a type of Animal. Each level inherits traits from the one above it: all animals breathe, all mammals are warm-blooded, and all dogs bark.

Read the lesson

Given the Vehicle class below, create a Car subclass that:

  • Adds a doors attribute (set in __init__)
  • Calls the parent __init__ using super()
  • Create a Car with make 'Toyota', year 2023, and 4 doors. Print its make, year, and doors on separate lines.

    class Vehicle:
        def __init__(self, make, year):
            self.make = make
            self.year = year
    class Vehicle:
    def __init__(self, make, year):
    self.make = make
    self.year = year
    # Create your Car subclass here

    More Inheritance exercises

    All Python exercises · 50 practice problems · Object-Oriented Programming quiz