Abstract
Quote
Many Forms, One Purpose.
- Think of polymorphism like a chameleon. A chameleon can change its appearance to blend in with its surroundings. Similarly, in programming, polymorphism lets objects take on different forms while still serving the same purpose
The Chameleon's Trick
Method overriding is the key to polymorphism’s magic. It lets you redefine how an object behaves, even if you don’t have access to the original code. This is like teaching a new trick to an old dog - you’re updating its behaviour without changing its core identity.
Coupled with dynamic binding, we are able to utilise the desired form from the many forms available during runtime to fulfil the task precisely. Thus, it allows us to write generic and succinct code based only on the compile-time type of the target. The actual behaviour of the code will be based on the run-time type of the target.
Future-proofing codes
With polymorphism, we can add new objects or features without breaking existing parts of your program.
Adding functionality challenge
If you want all animals to have a new ability, like
move
, you need to add that method to the baseAnimal
class and then override it in every existing animal class. This can be tedious and error-prone.