Polymorphism and Traffic Lights

Hi,

In this blog post I going to share with you how I understood
the concept of Polymorphism. I will not use complicated
technical language, as I want to make it fairly simple to understand.

First, what does the word polymorphism mean?
Poly -> More than one, multiple, many. ( e.g a polygon is many-sided )

20-sided_dice-thumb-430x433-119154

Morph -> Change or form. ( Morpheus the Greek God of dreams able to take any form ).

morpheus-chair

Nope, not this one but this:
morpheus-iris-01

Cool, with the help of this images your brain will assimilate this much faster and you probably will
never forget what these two words mean.

Imagine you’re going back home after a lovely day at work and
that you are an instance of object Person.
traffic_lights

As you can see on the image above, there are different objects (i.e people )
which are responding to the messages being sent from the TrafficLight object.
It does not matter if the objects have a green colour neither if they’re naked or not feeling well.What really matters is that each object responds to the message that TrafficLight object is sending out.

How do they respond to these messages ?
By agreeing on a protocol which in this case is:
See Message Passing

  • Do not move when message is “Red“.
  • Move forwards when message is “Green“.
  • Stop when message is “Orange“.

I do have to say that bugs can occur and some objects will behave differently, if they have
been overridden at some stage during the program execution ( i.e drunk driving ).
Some of them may be a work in progress object that they will probably stall out their vehicle.

Rationale:

Traffic light object sends a message to its known objects ( i.e people waiting by )
These objects know that if traffic light object sends a message saying
Red” they should respond to it by waiting until traffic light object sends them a message
saying “Green“.
How are they going to wait? That does not concern TrafficLight object, some of them might wait by looking around, or thinking about their lives. Once the “Green
message is received from TrafficLight object they know that they’re allowed to move.

I hope you understood what Polymorphism is in Computer Science in a less complicated
explanation.

ABC = Always Be Coding