class Animal:
def speak(self):
pass # placeholder
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
class Parrot(Animal):
def speak(self):
return "Squawk!"
# Polymorphism in action
animals = [Dog(), Cat(), Parrot()]
for animal in animals:
print(animal.speak())
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter