Implement a class hierarchy for a zoo, with a base class Animal
, and various child classes for different types of animals like Mammal
, Bird
, Reptile
. Each animal should have a name and a unique sound. Implement methods to print the name and the sound.
Example 1: Input:animal = Mammal("Elephant")
Output:Name: Elephant, Sound: Trumpet
Example 2: Input:animal = Bird("Parrot")
Output:Name: Parrot, Sound: Chirp
Use inheritance to create child classes for different animal types, and override the method to return the specific sound for each animal type.
class Animal: def __init__(self, name): self.name = name def sound(self): pass class Mammal(Animal): def sound(self): return "Trumpet" class Bird(Animal): def sound(self): return "Chirp" animal = Mammal("Elephant") print(f"Name: {animal.name}, Sound: {animal.sound()}") animal = Bird("Parrot") print(f"Name: {animal.name}, Sound: {animal.sound()}")
Unlock AI & Data Science treasures. Log in!