The Many Shapes of Polymorphism

What is Polymorphism?

The word “polymorphism” is from the Greek words “poly”, which means “many”, and “morph”, which means “form”. So “polymorph” means “many forms”, and “polymorphism” means “the ability to take on many forms”.

But enough academics.

How Does Polymorphism Work?

Polymorphism is a by-product of message-passing, or dynamic dispatching. One message may execute different code depending on the recipient, and in some OOP systems also depending on the arguments passed (this is called signature binding).

Polymorphism is only possible because of the object-oriented mechanism of message-passing; without message-passing, there can be no polymorphism.

Why is that?

It’s because message-passing does not directly identify the method to be executed; instead the receiving object gets to decide what method to execute in response to the message.

How is Polymorphism Useful?

The effects of polymorphism on code design serve to eliminate complex conditionals and thus clean up the code. Instead of having a bunch of if-statements or a switch-statement to decide what method to execute in certain circumstances, we rely move the code to different classes and rely on polymorphism to select the right one. This is why you’ll object-oriented programmers say that a switch statement is often an indicator of missing classes. This is especially true if the switch statement (or if-statements) are testing the type of object being operated upon. The effect of the switch statement would be achieved by polymorphism instead.

The Forms of Polymorphism

The simplest form of polymorphism is so-called “selector binding”. In this case, the name of the desired method is used by the receiving object to select the code to execute. In practical terms, the selection is done by the compiler or run-time environment, but we talk about the process as though the receiving object is doing it.

Personification or Anthropomorphism – treating inanimate things as if they were alive – is acceptable in object-oriented programming; in fact, it’s almost mandatory. We talk about objects selecting their own methods, and sending messages to each other. We talk about objects doing this and wanting that, and that’s ok. It is helpful to think of objects as having conversations with each other.

This encourages you to think in terms of the patterns of communication, instead of function-call hierarchies. The patterns of communication are called protocols, and we’ll talk about them next time.

I am off to South by Southwest Interactive in Austin, Texas, for the week, so there will be no Object Mechanics blog post next week.

Polymorphism is easy to see if the real world – people do the same things differently, or do different things and call them by the same names! For example, everyone’s handwriting is different, but we all call it “writing”. Everyone dances differently, but we still call it “dancing”. What other examples can you think of?

About Steven A. Lowe

I am an I.T. consultant, architect, developer, and author. In 1985 (or therebouts) I (re)discovered object-oriented programming and have applied its principles ever since (where appropriate, of course). I am the founder/CEO of Innovator LLC, a consulting and development company offering custom software development for business automation and process optimization.

Speak Your Mind

*