Python Programming - Polymorphism - Discussion

Discussion Forum : Polymorphism - General Questions (Q.No. 58)
58.
Consider the following Python code:
class Bird:
    def fly(self):
        return "Bird flying"

class Penguin(Bird):
    def fly(self):
        return "Penguin can't fly"
    
def display_flying_ability(bird):
    return bird.fly()

bird = Bird()
penguin = Penguin()

print(display_flying_ability(bird))
print(display_flying_ability(penguin))
Bird flying\nPenguin can't fly
Penguin can't fly\nBird flying
Bird flying\nBird flying
Penguin can't fly\nPenguin can't fly
Answer: Option
Explanation:
The display_flying_ability() function demonstrates polymorphism, accepting both Bird and Penguin instances and producing different outputs based on their specific implementations of the fly() method.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.