Python Programming - Polymorphism - Discussion

Discussion Forum : Polymorphism - General Questions (Q.No. 78)
78.
What is the output of the following Python code?
class Animal:
    def make_sound(self):
        return "Generic animal sound"

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

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

def pet_sounds(animals):
    for animal in animals:
        print(animal.make_sound())

dog = Dog()
cat = Cat()

pet_sounds([dog, cat])
Generic animal sound\nWoof!\nMeow!
Woof!\nMeow!\nGeneric animal sound
Woof!\nMeow!\nMeow!
Generic animal sound\nGeneric animal sound\nGeneric animal sound
Answer: Option
Explanation:
The pet_sounds() function demonstrates polymorphism, printing different sounds based on the specific implementations of the make_sound() method in the Dog and Cat classes.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.