Polymorphism
Prerequisites: Inheritance
Polymorphism means different classes can define a method with the same name, and calling code can use any of them the same way, without caring which specific class it actually is.
class Dog:
def speak(self):
return "Woof"
class Cat:
def speak(self):
return "Meow"
for animal in [Dog(), Cat()]:
print(animal.speak())
Expected output
Woof
Meow
The loop doesn't check "is this a Dog or a Cat?" — it just calls
.speak() on each, and each object knows how to respond correctly for
its own type.