πΆ Section 8: Create Species Subclasses (Dog, Cat, Bird)
π Summary (What you will do)
In this section, you will bring your game to life by creating real pet species. You will:
- Make three subclasses:
Dog,Cat, andBird - Override methods to customize how each species behaves
- Use
super()to build on the base class logic - Add unique
special_action()behavior for each pet
This is where polymorphism becomes visible: the same action can behave differently depending on the animal.
β Checklist (You must complete these)
- Open
pet_manager.py - Find the end of
class Animal - Directly below it, add the
Dog,Cat, andBirdsubclasses - Type the code by hand so you understand how inheritance works
β No new constants/settings are added in this section, so you do not need to edit the top-of-file constants.
π Core Concepts (New learning for this section)
1) Inheritance
Dog, Cat, and Bird inherit from Animal.
That means they automatically get all the shared stats and methods from the base class.
Example:
Dogautomatically hasfeed(),play(),clean(), andtick()- We only change the parts that should be different
2) Overriding methods
When a subclass defines a method with the same name, it overrides the parent method.
This is how each species gets its own personality:
- Dogs play better (
play_power) - Cats clean less (
clean_power) - Birds get messy faster (
_decay_cleanliness)
3) Calling super()
Sometimes you want to reuse the parent logic and just adjust it.
Thatβs what super() does:
- It calls the parent version of a method
- You can add or subtract from it to customize behavior
4) Polymorphism in action
Every species has a special_action(), but each one does something different.
The program can call pet.special_action() without caring which species it is β the correct version runs automatically.
π» Code to Write (Type this by hand in pet_manager.py)
Directions:
- Open
pet_manager.py - Scroll to the end of
class Animal - Directly below it, type the following code by hand:
π§ Code Review & Key Concepts (What important lines do)
class Dog(Animal)
This creates a subclass of Animal.
Dogs inherit all shared behavior, then override the parts that are different.
Overriding play_power()
def play_power(self) -> int:
return 24
Dogs get a bigger happiness boost when playing.
Using super()
return super()._decay_happiness() - 2
This starts with the base decay value, then makes dogs lose happiness more slowly.
special_action()
Each species has a unique special action:
- Dog: fetch (big happiness, more hunger)
- Cat: purr (happiness + cleanliness)
- Bird: sing (happiness up, hunger down)
Even though the method name is the same, each class behaves differently.
π§ͺ Test File: s08_test.py
β Create this file
Create a new file in the same folder as pet_manager.py called:
s08_test.py
π» Code to write in s08_test.py
π§ What this test is doing (and how it works)
- We create one
Dog, oneCat, and oneBird - We check that overridden methods return the correct values
- We run each
special_action()to make sure polymorphism works
β Run the test:
python s08_test.py
If each print shows the correct values and actions, your subclasses are working.

