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