Polymorphism - Define In Just Two Sentences [close

2019-01-04 15:40发布

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

30条回答
甜甜的少女心
2楼-- · 2019-01-04 16:39

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

查看更多
甜甜的少女心
3楼-- · 2019-01-04 16:40

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-04 16:41

I think implementation of methods of the same signature in different classes (having some sort of inheritance relation either using extends or implements) is method overriding and also polymorphism because in this way we are achieving many forms of the same method signature.

查看更多
Fickle 薄情
5楼-- · 2019-01-04 16:42

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

查看更多
再贱就再见
6楼-- · 2019-01-04 16:44

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.

查看更多
甜甜的少女心
7楼-- · 2019-01-04 16:45

Polymorphism is the ability of a function to automatically adapt to accept input data of different data types. You can 'Add' two doubles '1.1' and '2.2' and get '3.3' or 'Add' two strings "Stack" and "Overflow" and get "StackOverflow".

查看更多
登录 后发表回答