Demo of runtime polymorphism through method overriding

This commit is contained in:
Donald Calloway 2025-09-24 09:11:19 -07:00
parent 490d1aac42
commit 555a1c30ca
2 changed files with 19 additions and 1 deletions

View File

@ -12,4 +12,3 @@ def animal_sound(animal):
print(f" My dog says, {animal_sound(Dog())}") # Woof!
print(f" My cat says, {animal_sound(Cat())}") # Meow!

19
runtime_polymorphism.py Normal file
View File

@ -0,0 +1,19 @@
# Demonstrating runtime polymorphism through method overriding
class Animal:
def speak(self):
return "Some sound"
class Bird(Animal):
def speak(self):
return "Tweet"
class Dog(Animal):
def speak(self):
return "Woof"
print(Animal().speak()) # Some sound
print(Bird().speak()) # Tweet
print(Dog().speak()) # Woof