🐢 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, and Bird
  • 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, and Bird subclasses
  • 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:

  • Dog automatically has feed(), play(), clean(), and tick()
  • 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:

  1. Open pet_manager.py
  2. Scroll to the end of class Animal
  3. Directly below it, type the following code by hand:

Code image: s08-code


🧠 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

Code image: s08-test

🧠 What this test is doing (and how it works)

  • We create one Dog, one Cat, and one Bird
  • 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.