Polymorphism

Polymorphism lets different classes respond to the same method call in their own way, so one piece of code can work with many object types.

General Programming

The word means 'many forms'. Calling speak() on different animal objects gives different sounds, even though the calling code is identical. This makes programs flexible and extensible.

Examples

Python

class Cat:
    def speak(self): return "Meow"
class Dog:
    def speak(self): return "Woof"

for animal in [Cat(), Dog()]:
    print(animal.speak())

Output:

Meow
Woof

Learn it in a lesson

See also

Back to the Dictionary