Skip to content
Crayon CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

Lesson 23 of 26 · about 15 min

Inheritance

A subclass is a more specific version of a class. It inherits everything from its parent and can add or change behavior.

🧭 What you need first: Objects and classes

The story

All vehicles have wheels, a speed and a way to drive(). A bus is a vehicle plus a passenger count. An ambulance is a vehicle plus a siren. You wouldn’t describe wheels and speed from scratch for each one; you’d say “an ambulance is a vehicle, with these extras.”

That’s inheritance. The general class (Vehicle) is the parent; the specific one (Ambulance) is the child or subclass. The child gets everything the parent has, can add new things, and can change how some things work.

Another way: job roles

Every employee has a name, salary and can request_leave(). A Manager is an employee who can also approve_leave(). A Contractor is an employee whose request_leave() works differently. Shared things are written once, in Employee.

Another way: phone models

A new phone model keeps everything the last one did and adds a better camera. It “inherits” the old design and “overrides” the camera part.

Play with it

One call, different answers

Step through and notice: the loop calls speak() the same way on every pet, but each class answers differently. Cat didn’t define describe, so it uses the one it inherited from Animal.

Watch the code run, one step at a time

  1. 1class Animal:
  2. 2 def __init__(self, name):
  3. 3 self.name = name
  4. 4 def speak(self):
  5. 5 return "..."
  6. 6 def describe(self):
  7. 7 return self.name + " says " + self.speak()
  8. 8
  9. 9class Dog(Animal):
  10. 10 def speak(self):
  11. 11 return "Woof"
  12. 12
  13. 13class Cat(Animal):
  14. 14 def speak(self):
  15. 15 return "Meow"
  16. 16
  17. 17for pet in [Dog("Rex"), Cat("Tom"), Animal("Blob")]:
  18. 18 print(pet.describe())

➜ yellow = the line about to run · ✓ = the line that just ran

Name it

The real words

  • Inheritance: class Child(Parent): creates a subclass. Use it for an “is a” relationship: a Dog is an Animal. (A Car has an Engine, which is different: that’s an attribute, not inheritance.)
  • The subclass inherits every method and attribute of the parent. When you call a method, Python looks in the object’s own class first, then its parent, then its parent, and so on.
  • Overriding: defining a method with the same name in the subclass replaces the parent’s version for that subclass.
  • super() refers to the parent class. Common in __init__:
class Manager(Employee):
    def __init__(self, name, salary, team):
        super().__init__(name, salary)   # let Employee set up its part
        self.team = team                 # then add the extra bit
  • Polymorphism (“many forms”): code written for the parent type works with any subclass. A function that calls pet.speak() doesn’t need to know which animal it has.
  • isinstance(rex, Animal) is True. Every class ultimately inherits from Python’s built-in object.
  • Use inheritance sparingly. Deep family trees of classes get hard to follow. Many designers prefer simple classes that contain other objects.
Build it

Shapes that know their area

Shape has a name and an area() that returns 0. Make Circle(radius) and Square(side) subclasses that override area(). Use super().__init__ to set the name ('circle' / 'square'). Then total_area(shapes) adds up the areas of any list of shapes.

Challenge

Is-a or has-a?

Which pair should use inheritance?

Remember it

Cat(Animal) doesn't define describe(). What happens when you call tom.describe()?

Got it?

Finish the lesson to color it in on your map and collect its concept cards.

Did this lesson click?