class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
This method will be overridden by child classes
pass
def introduce(self):
return f"Hi! I am {self.name}"
class Dog(Animal):
def make_sound(self):
return "Woof! Woof!"
def fetch(self):
return f"{self.name} is fetching the ball"
class Cat(Animal):
def make_sound(self):
return "Meow! Meow!"
def climb(self):
return f"{self.name} is climbing a tree"
class Duck(Animal):
def make_sound(self):
return "Quack! Quack!"
def swim(self):
return f"{self.name} is swimming in the pond!"
List of animals (demonstrating polymorphism)
animals = [
Dog("Buddy"),
Cat("Whiskers"),
Duck("Donald")
]
Each animal introduces itself and makes its own sound
for animal in animals:
print(f"{animal.introduce()}: {animal.make_sound()}")