Wrong question
You’re thinking about the wrong question. It’s right to think about a problem and look for the right tools to solve it in the best way. One of these tools is polymorphism. And there are different mechanisms to meet this demand, each language implements in one way (or more than one).
The most important thing is not to do polymorphism where you don’t need it. In fact do not do object orientation where you do not need this orientation (which may be called paradigm, albeit there is doubt if it is). People tend to get attached to tools and want to use them anyway. It’s kind of like buying a cooking torch and wanting to use it for things that aren’t cream brolè (Okay, it’s good for some other things, but few, not to go out burning anything).
So understand the polymorphism and apply it when you need it.
What is polymorphism
A definition of polymorphism is the ability of something to be executed according to the state (or its type) at a given time. Use it when you need it. So it’s the ability to take various forms.
Another way to define him is to be one replacement of a conditional (usually if
or switch
) to decide which behavior or state to use at a given time. This is one of the reasons that is said to be best program for interfaces.
Polymorphism can be solved at compile or run time, depending on the mechanism used and the need.
Mechanism
In general you implement a problem and use polymorphism as a mechanism, so you can’t tell how to implement it. Unless you want to implement the mechanism. But almost nobody wants to, usually it is only necessary that you are creating a language. Has a question that shows a little this.
Even if you’re talking about using the mechanism depends on the language, cannot answer generically.
Another widely used mechanism, but it may be implemented in several ways. And it has a shape that’s a specialization of polymorphism, but we give another name. There may be others, but these are the most commonly used and documented, so the 3 forms presented are the most correct for the general cases.
Where to learn and see examples
We can show what it is to understand and then apply when necessary. I and other people have already responded about it. If you still have specific questions you can open new questions about what was missing. I think the best answer would be this mine.
There’s a question with several links for other answers on the subject.
And finally a example of use that I won’t put here because that has already been answered. Other examples. I put a classic example, although this is not exactly how it is used in practice, it gives a good understanding. And lastly.
I suggest checking the good cases of using polymorphism in the real world, for example the hierarchies of Views in UI frameworks. Object orientation (and polymorphism along with it) are highly hyped, winning frameworks such as iOS (Cocoa Touch) are notoriously "horizontal", meaning they have no deep class hierarchies, nor do they use polymorphism for everything, so it’s nice to also look for situations where it doesn’t fit.
– epx