Year 1 · Week 15
Chapter 15: Inheritance — The Family Tree
Last week you designed your own class from scratch. This week you will learn how to create a child class that automatically gets everything from a parent class — and then adds its own tricks. This is called inheritance, and it is one of the most powerful ideas in programming.
Session 6: Inheritance — The Family Tree
Duration: 60 minutes
Learning Goals
- Define a child class that inherits from a parent class
- Explain that a child class automatically gets all parent methods
- Add a new method that only exists on the child class
Warm-Up — Family Traits (10 min)
In programming it works the same way:
- Pikachu and Raichu are both Pokémon — they both inherit HP, damage, the attack method...
- But Raichu has extra attributes and methods that baby Pikachu doesn't.
# Parent class = Pokemon # Child class = ElectricPokemon # ElectricPokemon gets everything Pokemon has # ...plus its own special abilities!
Building the Parent Class — Animal (15 min)
We start with a simple Animal class. It has three methods: eat(), sleep(), and __str__().
class Animal: def __init__(self, name, hp): self.name = name self.hp = hp def eat(self): self.hp += 10 print(f'{self.name} eats. HP: {self.hp}') def sleep(self): print(f'{self.name} is sleeping...') def __str__(self): return f'{self.name} (HP: {self.hp})'
Let's test it:
cat = Animal('Kitty', 50) print(cat) # Kitty (HP: 50) cat.eat() # Kitty eats. HP: 60 cat.sleep() # Kitty is sleeping...
Creating a Child Class — Dog (25 min)
Now write a Dog class that inherits everything from Animal:
class Dog(Animal): # Dog inherits from Animal pass # empty — nothing here! rex = Dog('Rex', 80) print(rex) # Rex (HP: 80) ← uses Animal's __str__ rex.eat() # Rex eats. HP: 90 ← uses Animal's eat() rex.sleep() # Rex is sleeping... ← uses Animal's sleep()
Adding Child-Only Methods
A Dog inherits everything an animal can do — but dogs have special tricks. Let's add bark() and fetch():
class Dog(Animal): def bark(self): print(f'{self.name} says: WOOF!') def fetch(self, item): print(f'{self.name} fetches the {item}!') rex = Dog('Rex', 80) rex.bark() # Rex says: WOOF! rex.fetch('ball') # Rex fetches the ball! rex.eat() # Still works — inherited from Animal
Adding Child-Only Attributes
What if a dog has its own attribute? We can add one inside Dog's __init__:
class Dog(Animal): def __init__(self, name, hp, breed): super().__init__(name, hp) # call Animal's __init__ self.breed = breed # Dog's own attribute def bark(self): print(f'{self.name} the {self.breed} says: WOOF!') def fetch(self, item): print(f'{self.name} fetches the {item}!') rex = Dog('Rex', 80, 'Golden Retriever') rex.bark() # Rex the Golden Retriever says: WOOF! rex.eat() # Rex eats. HP: 90 ← inherited from Animal
Pair Exercise (10 min)
Following the same pattern, add a second child class Cat(Animal). Requirements:
Catinherits fromAnimal- Add a
purr()method — prints "___ purrs softly..." - Add a
scratch(target)method — reducestarget's HP by 5 - Create one
Dogand oneCat, and have them interact (dog barks, cat scratches)
Show Example Answer
class Cat(Animal): def purr(self): print(f'{self.name} purrs softly...') def scratch(self, target): target.hp -= 5 print(f'{self.name} scratches {target.name}! HP -5') # Create one dog and one cat rex = Dog('Rex', 80) whiskers = Cat('Whiskers', 40) rex.bark() # Rex says: WOOF! whiskers.scratch(rex) # Whiskers scratches Rex! HP -5 print(rex) # Rex (HP: 75) whiskers.purr() # Whiskers purrs softly... whiskers.eat() # Whiskers eats. HP: 50 ← inherited from Animal
Draw the Family Tree
On paper, draw the inheritance relationships we created today:
- Write
Animalat the top. List its methods:__init__,eat,sleep,__str__ - Below it, draw two boxes:
DogandCat - Draw arrows from
Animaldown toDogandCat - In the
Dogbox, write its own methods:bark,fetch - In the
Catbox, write its own methods:purr,scratch - Use a different color to mark which methods are inherited vs. child-only
Exit Ticket
- What does
class Dog(Animal):mean? Explain in your own words. - If
Animalhas 3 methods andDogadds 2 more, how many methods does aDogobject have? - Can a plain
Animalobject callbark()? Why or why not?