Animal Sounds with Inheritance
Medium
Create a base class Animal with a method speak() that returns "..." . Create subclasses Dog and Cat that override speak() to return "Woof!" and "Meow!" respectively. Write a function make_sound(animal) that calls and returns animal.speak(), demonstrating polymorphism.
Input
No input — create instances of Dog and Cat.
Output
The sounds returned by each animal.
Example
Input
make_sound(Dog()), make_sound(Cat())
Output
Woof! Meow!
Override the speak() method in each subclass. make_sound() should work the same for any Animal subclass without checking its type.
15 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.