Demo of runtime polymorphism through method overriding
This commit is contained in:
parent
490d1aac42
commit
555a1c30ca
@ -12,4 +12,3 @@ def animal_sound(animal):
|
|||||||
print(f" My dog says, {animal_sound(Dog())}") # Woof!
|
print(f" My dog says, {animal_sound(Dog())}") # Woof!
|
||||||
print(f" My cat says, {animal_sound(Cat())}") # Meow!
|
print(f" My cat says, {animal_sound(Cat())}") # Meow!
|
||||||
|
|
||||||
|
|
||||||
19
runtime_polymorphism.py
Normal file
19
runtime_polymorphism.py
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user