What is Polymorphism & How is it implemented?

Polymorphism refers to the ability of two or more objects belonging to different classes to resepond exactly to the same message in class specific ways.

Note : If you search the web for polymorphism, you will find many many definitions. I even read somewhere that overloading is also a kind of polymorphism.  But  a majority of the sites say the definition similar to the above.  So, I am sticking to it.  Remember there is no hard core definition for any of the object oriented concepts.  That is the problem why people run away from basics.

Example :   I would like to give two real time examples for polymorphism. 

1. School Bell.  Lets say that the final bell rings in a school/college.  What happens – a group of students go home, a group of students go to play ground, a group of students go to library and a group of stundents go to special classes.  What do professors do. A group of professors go home, a group of professors proceed to lab to continue their research, a group of professors do admin work etc.  The message ( ie school final bell) is same for all the objects (students & professors. note that professors and students are two different types of objects), but the way they respond depends on the object.   This is a very good example of polymorphsim

2. If you give an instruction "cut' to three different people – a hair stylist, a doctor and a movie actor. How do they respond.  The hair stylist cuts the hair.  A doctor will cut your body for surgery, a movie actor will stop acting. The message (cut) is same to all three objects ( actor, hair stylist and a doctor), but they threee respond in three different ways. This is called as polymorphism.

Inheritance with overriding facilitates polymorphism.  

Lets say that we have a base class called Class A and it has a overridable method called print which prints all the attributes.  Then by inheritance concept, all the dervied class will inherit this print method. The derived classes can either override the print method or use the print method from base class.  So, all the objects of base class and derived class have a method called print and they respond differently to a message based on its class implementation.

How is polymorphism implemented ?
 The fundamental idea of polymorphism is that a compiler doesnt know which method (ie base class implementation or derived class implementation of a method)  to call during  the compile time.  It only knows during run time ( based on the  object).   This is called as latebinding.  This type of polymorphism is called as "polymorphism by inheritance"

Polymorphism can also be achived through method overloading.  During the compile time itself the compiler knows which method to execute. This is known as early binding. 

Advertisements

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

22 thoughts on “What is Polymorphism & How is it implemented?”

  1. two examples are related to run time polymorphism . . . Over loading is also a type of polymorphism , we can’t neglect it. . Anyways good example .

  2. Hello.i want to know to about the Clear difference between. Polymorphism ,Overriding , Virtual Function, Abstract and inheritance. I am confused between these concepts because all of these are interrelated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s