Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Build a Multi-Level Class Hierarchy with Shared Behavior

Design a multi-level class hierarchy using inheritance, method overriding, and a behavior mixin; implement polymorphic methods and use super().

Python practice25 minInheritance & PolymorphismAdvancedLast updated April 8, 2026

Problem statement

You will implement a small multi-level object-oriented hierarchy in Python. The base class is Creature, extended by Animal, which is extended by Mammal. Dog and Cat should inherit from Mammal and a mixin PetBehavior that provides shared pet-specific behavior. Implement constructors and methods so that: - Creature stores name and age and provides a basic info() string. - Animal adds a species attribute and a describe() method that uses name, species, and age. - Mammal sets a class attribute indicating warm-blooded animals and provides a move() method. - PetBehavior is a mixin that provides a play() method using the instance's name. - Dog and Cat set their species appropriately, implement speak() returning 'woof' or 'meow', and override describe() to include what the pet says (use super() in your override). Also provide factory functions create_dog(name, age) and create_cat(name, age) that return instances, and implement collect_sounds(pets) which accepts an iterable of pet instances and returns a list of the results of calling speak() on each pet. Your implementations should demonstrate multi-level inheritance, method overriding, use of super(), and shared behavior via the mixin.

Task

Create a class hierarchy (Creature -> Animal -> Mammal -> Dog/Cat) plus a PetBehavior mixin. Implement methods using super(), override behavior in subclasses, and write a helper that uses polymorphism to collect sounds.

Examples

Dog description includes species, age and sound

Input

create_dog("Fido", 3).describe()

Output

Fido is a dog aged 3 and says woof

Dog.describe() uses Animal.describe() via super() and appends the pet's speak() result.

Input format

This task defines classes and functions. Tests will call factory functions and instance methods, e.g., create_dog("Name", 3).describe().

Output format

Return values from methods (strings or lists). The test harness compares the string form of the returned value.

Constraints

Use class inheritance for the hierarchy. Use super() when overriding describe() in Dog and Cat. PetBehavior should be a separate class used as a mixin. collect_sounds must work for any iterable of objects that implement speak().

Samples

Sample 1

Input

create_cat("Mittens", 4).speak()

Output

meow

Cat.speak() returns the sound 'meow'.