Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create an Abstract Base with Concrete Subclasses

Define an abstract base class and implement multiple concrete subclasses that follow the required interface.

Python practice15 minInheritance & PolymorphismIntermediateLast updated April 8, 2026

Problem statement

You must design an abstract Animal base class that enforces a speak() method. Implement three concrete subclasses (Dog, Cat, Bird) that provide specific speak() implementations. Also write a factory function create_animal(kind, name) that returns an instance of the appropriate subclass. The factory should be case-insensitive for kind and default the name to 'Unknown' if an empty string is given.

Task

Implement an abstract Animal base class with concrete Dog, Cat, and Bird subclasses plus a factory function that creates them. Ensure names default correctly and kind matching is case-insensitive.

Examples

Make and speak as a dog

Input

create_animal('dog', 'Rex').speak()

Output

Rex says woof

create_animal returns a Dog instance. speak() returns a string using the instance name and the dog's sound.

Input format

Function-style calls. Example: create_animal('cat', 'Mittens').speak()

Output format

Return the string produced by calling speak() on the created instance.

Constraints

Do not change the names of the classes or the create_animal function. The factory must accept case-insensitive kinds. If name is empty, use 'Unknown'.

Samples

Sample 1

Input

create_animal('bird', 'Tweety').speak()

Output

Tweety says tweet

Birds say 'tweet' in this exercise.