I've looked at other definitions and explanations and none of them satisfy me. I want to see if anybody can define polymorphism in at most two sentences without using any code or examples. I don't want to hear 'So you have a person/car/can opener...' or how the word is derived (nobody is impressed that you know what poly and morph means). If you have a very good grasp of what polymorphism is and have a good command of English than you should be able to answer this question in a short, albeit dense, definition. If your definition accurately defines polymorphism but is so dense that it requires a couple of read overs, then that's exactly what I am looking for.
Why only two sentences? Because a definition is short and intelligent. An explanation is long and contains examples and code. Look here for explanations (the answer on those pages are not satisfactory for my question):
Polymorphism vs Overriding vs Overloading
Try to describe polymorphism as easy as you can
Why am I asking this question ? Because I was asked the same question and I found I was unable to come up with a satisfactory definition (by my standards, which are pretty high). I want to see if any of the great minds on this site can do it.
If you really can't make the two sentence requirement (it's a difficult subject to define) then it's fine if you go over. The idea is to have a definition that actually defines what polymorphism is and doesn't explain what it does or how to use it (get the difference?).
Definition:
Polymorphism is a $10 word for a $1 idea - that when I ask for something to be done, I don't care how it is achieved as long as the end result is appropriate. As long as the service is provided correctly, I don't care about the implementation.
Discussion
While it's commonly used in software development, especially in systems developed following object oriented principles, Polymorphism is fundamentally a real world principle and should be defined in real world terms, not technological ones.
Examples
When I want to make a phone call, I pick up a phone, dial a number and talk to the party at the other end. I don't care about who made the phone, what technology it uses, whether it's wired, wireless, mobile or VOIP, or whether it's under warranty.
When I want to print a document, I print it. I don't care about the implementation language, brand of printer, style of connection, choice of consumable or quality of paper.
I just thought I'd add my own interpretation of what polymorphism is: Very generically, polymorphism is the act of providing a single interface to entities of different types.
That's rather generic, but that's the only way I can think of to wrap all three types of polymorphisms I know about: ad hoc, parametric and subtype. I'll go in more details below, and have sorted polymorphism types by name, alphabetically. The one you're interested on is most probably subtype polymorphism, which is the last one.
Ad hoc polymorphism
Ad hoc polymorphism is the act of providing multiple implementations of the same method for different parameter types. In OOP, it's generally known as method overloading. For example:
Both
format
methods share a single interface, but they work on entities of different types.Parametric polymorphism
Parametric polymorphism is the act of making a class (or method) work on a type that is itself a parameter of the class (or method). It's often referred to as generics.
For example, Java's
List[T]
expects a parameterT
at instantiation time, and this parameter defines the type of the resulting object.Note for purists that I'm purposefully ignoring raw types as I feel they'd just muddy the waters in this context.
List[String]
andList[Date]
share a single interface, but work on (and are) different types.Subtype polymorphism
Subtype polymorphism is probably what you initially meant in your question: It's the act of providing a single interface to multiple implementations of the same type.
To use the customary example:
Animal
provides a contract that all implementations must respect.Dog
is anAnimal
, and as such supports all operations thatAnimal
declares. According to the Liskov substitution principle, this allows you to use an instance ofDog
where an instance ofAnimal
is expected (but not the other way around).If
Cat
andDog
are both subclasses ofAnimal
, then they share a single interface but are in fact different types.I'm going off in a bit of a tangent here, but subtype polymorphism is (I think) the only one that allows overriding: the act of redefining the behaviour of a method defined by a parent class. This is often confused with overloading which, as we saw before, is a type of polymorphism and doesn't in fact need subclassing (nor does it need classes, really).
Polymorphism is a programming feature that allows an object to have many types ('shapes') and lets you treat it as any of those types depending on what you need to do without knowing or caring about its other types.
I really understand, why you are asking this question. I understand polymorphism, but I was at a job interview and was asked to give short and clear definition of polymorphism. Because I couldn't give clear and short definition I started thinking about it and here is my definition:
The ability of objects of one type to have one and the same interface, but different implementation of this interface.
Polymorphism
Different objects can respond to the same message in different ways, enable objects to interact with one another without knowing their exact type.
Via: http://www.agiledata.org/essays/objectOrientation101.html
Polymorphism is a object oriented strategy used when designing object models, to help simplify the code. At it's core polymorphism is the ability to define two simillar yet different objects, and to then treat the two objects as if they are the same.
Ok that's hard....