views:

2391

answers:

19

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):

http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading
http://stackoverflow.com/questions/210460/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?).

+16  A: 

Fruit can be eaten, as a general rule, but different types of fruit is eaten in different ways. An apple, which is a fruit, can be eaten (because it is a fruit). A banana can also be eaten (because it is also a fruit), but in a different manner from an apple. You peal it first.

Well, at least I do, but I'm weird in some manners so what do I know.

This illustrates inheritance (fruit can be eaten), polymorphism (something that eats fruit can eat all types of fruit), and encapsulation (a banana has a skin).

Seriously though, object inheritance, polymorphism, encapsulation, virtual things, abstract things, private things, public things, these are all hard concepts. If someone absolutely wants to have a 2-sentence definition of this then please tag the question as a code-golf variant, because two such sentences will have to be so terse that unless you know what it is already you won't learn enough about it to know what you need to learn more about.

Lasse V. Karlsen
lassevk: "unless you know what it is already you won't learn enough about it to know what you need to learn more about" << Just to clarify, that's what I am expecting. I'm looking for a definition that may take some thought to understand. Not one that would be used to teach a beginner.
Mark Testa
I gathered that, I just posted a somewhat humorous (to me anyway) answer :) Polymorphism and OOP is one of those big wall-things, where if you graph the learning curve, you just hit a big wall and either you crawl over it, or you don't. If you do, then you usually have a big AHA! experience...
Lasse V. Karlsen
Hemlock is a fruit too! You can eat it but only once!
James Anderson
+3  A: 

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....

JoshBerke
+7  A: 

Polymorphism is declaring a uniform interface that isn't type aware, leaving implementation details to concrete types that implement the interface.

Eran Galperin
+4  A: 

Wikipedia: Polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. Pretty straightforward for me.

Otávio Décio
+16  A: 

Polymorphism allows one type to express some sort of contract, and for other types to implement that contract (whether through class inheritance or not) in different ways, each according to their own purpose. Code using that contract should not(*) have to care about which implementation is involved, only that the contract will be obeyed.

(*) In the ideal case, anyway - obviously quite often the calling code has chosen the appropriate implementation very deliberately!

Jon Skeet
Mark, did you at one point accept this answer and then unaccept it? I'm trying to work out what looks like a bug in the reputation system - this answer has given me a net of -15 rep for today, strangely enough.
Jon Skeet
Same here, Jon - I have now 2 accepted answers with -15 rep. Not that I care but it is intriguing.
Otávio Décio
ocdecio: Perhaps you could vote for http://stackoverflow.uservoice.com/pages/general/suggestions/97306-rep-limit-still-seems-to-apply-to-accepted-answers?
Jon Skeet
@Jon: great, just voted.
Otávio Décio
Rep system has been crazy lately. There are times in the day when my rep fluctuates wildly, going back and forth around a certain number.Also, a couple of days ago there was a major change in the rep of all the users (cron job?)
Eran Galperin
@Eran: See blog.stackoverflow.com for the majority of the changes. I think when they fixed the accepted answer stuff it didn't *quite* work.
Jon Skeet
@Jon: No, I haven't accepted any answers yet. Though it is a very good answer.
Mark Testa
@Mark: Thanks for the confirmation. That's valuable information.
Jon Skeet
@Jon: it definitely didn't work. I just had an answer accepted but received no reputation from that. It's not that big of a deal, I just wonder what's really going on
Eran Galperin
Yeah, rep is bouncing, but always restores. At least, it did for me.
Dykam
Strictly speaking, there is no requirement that "one type express some sort of contract". All that is really required is that multiple implementations can respond to the same message without the message sender needing to know or care which implementation is handling the message.
Doug Knesek
@Doug: If there's no contract, even implied through documentation or naming, then how on earth do you know it's going to do what you want it to? You talk about an "interface" in your own answer - which sounds very much like a contract to me - what do you see as the difference? Both "interface" and "contract" are words which can be used in a "strong" sense (e.g. enforced at compile-time) or very loosely (e.g. by naming convention and using dynamic typing).
Jon Skeet
A: 

Giving a single name to a set of analogous operations on different types. When done well, the analogy is obvious e.g. "adding" numbers arithmetically and "adding" strings by concatenation (which sums their lengths).

joel.neely
+3  A: 

Multiple forms of a single object is called Polymorphism.

milot
+8  A: 

Actually, there are multiple forms of polymorphism and there is quite some controversy over it; you may even see CS professors who cannot define it properly. I am aware of three types:

  • ad-hoc polymorphism (looks like a duck and walks like a duck => is a duck). Can be seen in Haskell and Python for example.

  • generic polymorphism (where a type is an instance of some generic type). Can be seen in C++ for example (vector of int and vector of string both have a member function size).

  • subtype polymorphism (where a type inherits from another type). Can be seen in most OO programming languages (i.e. Triangle is a Shape).

+1 for mentioning that there are different types of polymorphism. However, your definition of ad-hoc polymorphism seems to be quite different from the one mentioned at http://en.wikipedia.org/wiki/Type_polymorphism . That page says there are 2 types (ad-hoc versus parametric), not 3, and also make a distinction between polymorphic functions and polymorphic data types. Your 3 types, as far as I can determine correspond to parametric polymorphic functions, parametric polymorphic data types, and ad-hoc polymorphic functions, respectively.
Laurence Gonsalves
+3  A: 

Polymorphism is a software coding abstraction where several different underlying entities (usually data, but nit always) all share a common interface which allows them to look and act identical at runtime. We use this as a development technique to enforce consistent behavior over a wide range of similar, but not identical instances with an absolute minimal implementation, thus reducing the expectation for bugs and inconsistencies.

Paul.

Paul W Homer
+1  A: 

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

+1  A: 

polymorphism == multiple classes + same method signatures + class-specific behavior.

S.Lott
+1  A: 

A single class doing different methods is called polymorphism.

+4  A: 

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.

Bevan
+1  A: 

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 realization of this interface.

niki4ko
+1  A: 

Multiple implementations of the same interface.

Example: Many models of telephone implement the numeric keypad interface.

Doug Knesek
+1  A: 

This is the definition that I've always followed:

Two objects are polymorphic (with respect to a particular protocol) between them, if both respond to the same messages with the same semantic.

Polymorphism is about messages, is about being able to respond the same set of messages with the same semantic.

If two object CAN respond to empty? but the semantic of the message is different, then.. they are not polymorphic.

ClaudioA
A: 

Polymorphism at the lower level is the ability to invoke methods that are defined by the implementors of an interface from the interface instance.

Igor Zevaka
A: 

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.

Jeff Sternal
A: 

I guess sometimes objects are dynamically called. You are not sure whether the object would be a triangle, square etc in a classic shape poly. example.

So, to leave all such things behind, we just call the function of derived class and assume the one of the dynamic class will be called.

You wouldn't care if its a sqaure, triangle or rectangle. You just care about the area. Hence the getArea method will be called depending upon the dynamic object passed.