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.
Good Work Dude…!!!!
Again, this is really clearly explained, thanks a lot,
Tom
Thanks
Very good document to undestand ploymorphism .
Dhanya
Really good work dude
thanks for the document.
really usefull for a newbie lik me..!
nice one. thanks.
Good examples.
You all of us, in which company?
I wants information about msc and what i do after B.Sc.(I.T.).
Have nice day.
information send on following e-mail ID,
nivruttipatil@rocketmail.com