Legends ofPythos
Claim your name
Objects and Classes

Inheritance

Lesson 8 of 13

Watch the lesson1:17 · 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 behaviors 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

class Car(Vehicle): # 'Car' is a subclass of '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 specialized 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 behavior

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

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

civic = Car('Honda', 2)
print(civic.make)   # Honda (from parent)
print(civic.doors)  # 2     (specific to child)
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. Using super() is like saying: "Do your part first, then I'll add my extras."

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 (it's specifically a Car)
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 brand. Then create a subclass Car(Vehicle). Give it one method called honk() that returns 'Beep!'. Create one instance, my_car = Car('Toyota'), and print its honking sound.
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+40 XP
Define a parent class Shape with an attribute called color. Create a subclass Triangle(Shape) that adds one extra instance attribute: sides, set to the integer 3. Instantiate it as t = Triangle('blue') and print both its color and side count.
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.