Animal Class Hierarchy

Implement a class Animal and then create child classes Dog, Cat, Bird, each having a unique method sound that returns the sound they make.

Example 1:

Input: Dog 
Output: "Woof!"

Example 2:

Input: Bird 
Output: "Chirp!"

Implement a method sound in each of the child classes that returns the respective sound of the animal.

class Animal:
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Woof!"

class Cat(Animal):
    def sound(self):
        return "Meow!"

class Bird(Animal):
    def sound(self):
        return "Chirp!"

# Test the classes
dog = Dog()
print(dog.sound())  # Output: Woof!

bird = Bird()
print(bird.sound())  # Output: Chirp!

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!