Q:

Can someone explain Polymorphism to me?

Possible Duplicate:
Try to describe polymorphism as easy as you can 

I've never been able to fully comprehend what polymorphism is. Can someone explain, perhaps with use of an example, what it is and how it works? Just the basics.

A:

Perhaps it's easiest to start with a non-computer analogy.
Consider if you told somebody to "Go to the store and buy some of your favorite food for supper."
If you said this to a 14 year-old son, he'd probably ride his bike to the store, have to pay cash for the food, and you'd be having pizza for supper.
If you said it to your wife, she'd probably drive to the store, use a card to pay for the food, and you might be eating chicken Cordon Bleu with Chardonnay instead.
In a program, things work out a bit the same way: you specify something at a relatively abstract level (go to the store and get supper). Each object provides its own concrete implementation of how to implement that, and in many cases provides for some variation in exactly what it's going to do (e.g., like the differences in favorite foods above).
Of course, when you're programming, most of that requires a specification that's a lot more detailed and unambiguous. The general idea remains the same though. For the scenario above, you might have a person base class (or interface) that defined methods like go to store and select favorite food and pay for purchase. You'd then have implementations of that like adult and teenager, each of which defined its own method of going to the store, selecting favorite food, and paying for a purchase. Those methods would be polymorphic, because each implementation would have its own way of carrying out the higher-level command you gave.

A:

Literally, polymorphism means "having multiple forms". In programming, if a variable can hold more than one type of value, then that's a kind of polymorphism. If functions can process more than one type of parameter, that's also polymorphism.
Object oriented languages have polymorphism through the class hierarchy: a reference to a base class or interface can refer to multiple types of object, as long as those other types are derived from the base. This is called subtype polymorphism.
Generic programming is another kind of polymorphism. By applying parameters to types, the same bit of code can handle multiple types of object. This is called parametric polymorphism.
Operator overloading, and overloading of methods within a class are another kind of polymorphism, known as ad hoc polymorphism, because it's less systematic than parametric or subtype polymorphism.

