I was watching a Google Tech Talks video, and they frequently referred to polymorphism.
What is polymorphism, what is it for, and how is it used?
I was watching a Google Tech Talks video, and they frequently referred to polymorphism.
What is polymorphism, what is it for, and how is it used?
Let's use an analogy. For a given musical script every musician which plays it gives her own touch in the interpretation.
Musician can be abstracted with interfaces, genre to which musician belongs can be an abstrac class which defines some global rules of interpretation and every musician who plays can be modeled with a concrete class.
If you are a listener of the musical work, you have a reference to the script e.g. Bach's 'Fuga and Tocata' and every musician who performs it does it polymorphicaly in her own way.
This is just an example of a possible design (in Java):
Usually this refers the the ability for an object of type A to behave like an object of type B. In object oriented programming this is usually achieve by inheritance. Some wikipedia links to read more:
EDIT: fixed broken links.
Polymorphism literally means, multiple shapes. (or many form) : Object from different classes and same name method , but workflows are different. A simple example would be:
Consider a person X.
He is only one person but he acts as many. You may ask how:
He is a son to his mother. A friend to his friends. A brother to his sister.
From Understanding and Applying Polymorphism in PHP, Thanks Steve Guidetti.
I know this is an older question with a lot of good answers but I'd like to include a one sentence answer:
There are plenty of examples above that show this in action, but I feel this is a good concise answer.
Polymorphism => Different execution according to instance of class, not type of reference variable.
A interface type reference variable can refer any of the class instance that implements that interface.