Python Programming - Polymorphism - Discussion

Discussion Forum : Polymorphism - General Questions (Q.No. 56)
56.
What does the following Python code demonstrate?
class Animal:
    def speak(self):
        pass

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

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

def pet_sound(animal):
    return animal.speak()

dog = Dog()
cat = Cat()

print(pet_sound(dog))
print(pet_sound(cat))
Method overloading
Method overriding
Operator overloading
Polymorphism
Answer: Option
Explanation:
The pet_sound() function demonstrates polymorphism as it can accept different types of animals (instances of Dog and Cat) and produce different sounds based on their specific implementations.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.