Legends ofPythos
Claim your name
Objects and Classes

Inheritance

Lesson 8 of 13

Watch the lesson5:22 · with Torsten
Think of inheritance like a family recipe book. You have your grandmother's basic bread dough (the parent). When you want to make rye bread, you don't rewrite the whole process from scratch; you start with her base recipe and add caraway seeds (the subclass). In Python, this means one class can borrow all the setup code and behaviours of another.
Why it matters: Imagine building a game. You need Player, Enemy, and NPC classes. They all have health, position, and movement logic. Inheritance lets you define that shared logic once in a base class so your code stays clean.

The syntax

class Vehicle:
    def __init__(self, make):
        self.make = make

# Car is a subclass of Vehicle
class Car(Vehicle):
    pass
Car inherits everything from Vehicle. You don't need to write any code inside the body.
The parentheses (Vehicle) tell Python that Car is a specialised type of Vehicle. Because it inherited, you can create a car without worrying about how its make is stored—that logic already exists in the parent.

Extending behaviour

class Vehicle:
    def __init__(self, make):
        self.make = make

class Car(Vehicle):
    # Car adds doors to the parent's setup
    def __init__(self, make, doors=4):
        # Run Vehicle's __init__ first
        super().__init__(make)
        self.doors = doors

civic = Car('Honda', 2)
print(civic.make)   # Honda (from Vehicle)
print(civic.doors)  # 2 (Car's own)
super().__init__(make) ensures the base class does its job first.
Here, Car adds a new piece of data (doors) but still needs the parent's logic for make. super() reaches the parent class, here Vehicle, so super().__init__(make) runs Vehicle's __init__, and the make gets stored. Using super() is like saying: "Do your part first, then I'll add my extras." The next lesson covers super() in full.

Type checking

class Vehicle:
    def __init__(self, make):
        self.make = make

class Car(Vehicle):
    pass

civic = Car('Honda')

print(isinstance(civic, Vehicle))  # True
print(type(civic) == Vehicle)      # False
isinstance checks if an object fits the category; type is exact.
A car is a vehicle, so isinstance returns True. But its specific type is still Car. This distinction helps when you want to accept any kind of transport in a function but need to know exactly what you're holding.

Your turn

0 of 3 solved

Exercise 1

+40 XP
Create a Vehicle parent class with an __init__(self, make) that stores the car's make (its brand) as self.make. Then create a subclass Car(Vehicle). Give it one method called honk() that returns 'Beep!'. Create one instance, my_car = Car('Toyota'), and print what my_car.honk() returns. Print nothing else.
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+40 XP
Define a parent class Shape whose __init__(self, color) stores its argument as self.color. Create a subclass Triangle(Shape) that adds one extra attribute, self.sides, set to the whole number 3; its own __init__ still takes the colour and passes it on to Shape's. Make one as t = Triangle('blue') and print both its colour and its number of sides.
Write your Python here.

Run your code to check it against the tests.

Exercise 3

+40 XP
The Vehicle and Car classes from the previous exercises are already in the editor. Create v = Vehicle('Ford') and c = Car('Honda'), then print whether each of them is an instance of Vehicle. Two lines of output in total.
class Vehicle:
    def __init__(self, make):
        self.make = make


class Car(Vehicle):
    pass

Run your code to check it against the tests.