From 555a1c30ca2e10c5ce3d0e25661dfb15d634e24b Mon Sep 17 00:00:00 2001 From: Donald Calloway Date: Wed, 24 Sep 2025 09:11:19 -0700 Subject: [PATCH] Demo of runtime polymorphism through method overriding --- dynamic_polymorphism.py | 1 - runtime_polymorphism.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 runtime_polymorphism.py diff --git a/dynamic_polymorphism.py b/dynamic_polymorphism.py index c77ea2a..7162883 100644 --- a/dynamic_polymorphism.py +++ b/dynamic_polymorphism.py @@ -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! - \ No newline at end of file diff --git a/runtime_polymorphism.py b/runtime_polymorphism.py new file mode 100644 index 0000000..e82682c --- /dev/null +++ b/runtime_polymorphism.py @@ -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 \ No newline at end of file