DELAPARRA
Index
Color
NL
Category: Programming paradigms

Polymorphism

Explained by Updated

What is Polymorphism?

Polymorphism: Different objects respond to the same method call in their own way. Both Dog and Cat have a speak() method — Dog says "Woof", Cat says "Meow". Polymorphism means different objects can respond to the same method call in their own way. A Dog and a Cat both have speak(), but each makes its own sound. The calling code does not need to know which animal it has.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Polymorphism

How is Polymorphism used in practice?

In business software, this removes long if and switch chains. Instead of checking whether a payment is iDEAL, card or invoice before each action, every payment type implements the same interface, and the code simply calls $payment->process().

What should you keep in mind with Polymorphism?

Adding a new type then means adding a class, not editing every place that handles payments. Interfaces in PHP make this explicit and checkable. Polymorphism is one of the most practical object-oriented ideas, and a good sign to use it is the same switch on a type appearing in several places in your code.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with P

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.