tags:

views:

880

answers:

6

Hi,

I noticed that somewhere polymorphism just refer to virtual function. However, somewhere they include the function overloading and template. Later, I found there are two terms, compile time polymorphism and run-time polymorphism. Is that true?

My question is when we talked about polymorphism generally, what's the widely accepted meaning?

+2  A: 

Usually people are referring to run-time polymorpism in my experience ...

Goz
Thats also my opinion.
George B.
+1  A: 

In its most succinct form, polymorphism means the ability of one type to appear as if it is another type.

There are two main types of polymorphism.

  1. Subtype polymorphism: if D derives from B then D is a B.
  2. Interface polymorphism: if C implements an interface I.

The first is what you are thinking of as runtime polymorphism. The second does not really apply to C++ and is a really a concept that applies to Java and C#.

Some people do think of overloading in the special case of operators (+, -, /, *) as a type of polymorphism because it allows you to think of types that have overloaded these operators as replaceable for each other (i.e., + for string and + for int). This concept of polymorphism most often applies to dynamic languages. I consider this an abuse of the terminology.

As for template programming, you will see some use the term "polymorphism" but this is really a very different thing than what we usually mean by polymorphism. A better name for this concept is "generic programming" or "genericity."

Jason
Way to dodge the question. ;)
jalf
@Downvoter: Please explain. Thanks!
Jason
Your second type is just a special case of the first type. If C implements I then the type C is derived from I. In C++ you can get pretty much the same effect as a Java or C# interface by creating a class with only "pure virtual" methods.
Laurence Gonsalves
Downvoting because it doesn't answer the question. You have described similar variants of dynamic polymorphism. You haven't given any indication that you're even aware of what static polymorphism *is*.
jalf
@jalf: See my last paragraph.
Jason
Yep, it's wrong. :) It's a different thing than what people indoctrinated with Java mean by polymorphism, but the word has a long history being used to describe this in functional languages. OOP doesn't have a monopoly on the truth, you know. :)
jalf
@jalf: What is wrong in my last paragraph?
Jason
A: 

Yes, you are basically right. Compile-time polymorphism is the use of templates (instances of which's types vary, but are fixed at compile time) whereas run-time polymorphism refers to the use of inheritance and virtual functions (instances of which's types vary and are fixed at run time).

anon
Using "polymorphism" to apply to template programming and "polymorphism" to apply to subtype polymorphism is akin to using "football" to apply to two very different games.
Jason
I agree. But "football" *is* used to tag multiple ball games. English is like that. and "polymorphism" is similarly used.
anon
@Neil Butterworth: Sure, I was bit obtuse in what I meant. I think it's important to note that the use of "polymorphism" has two very different meanings in programming.
Jason
I don't think anyone uses just "polymorphism" where template programming is concerned. It always tends to be specified as "compile-time polymorphism" - by which it is meant "achieves sort of same thing as polymorphism, except at compile-time".
UncleBens
+5  A: 

Yes, you're right, in C++ there are two recognized "types" of polymorphism. And they mean pretty much what you think they mean

Dynamic polymorphism

is what C#/Java/OOP people typically refer to simply as "polymorphism". It is essentially subclassing, either deriving from a base class and overriding one or more virtual functions, or implementing an interface. (which in C++ is done by overriding the virtual functions belonging to the abstract base class)

Static polymorphism

takes place at compile-time, and could be considered a variation of ducktyping. The idea here is simply that different types can be used in a function to represent the same concept, despite being completely unrelated. For a very simple example, consider this

template <typename T>
T add(const T& lhs, const T& rhs) { return lhs + rhs; }

If this had been dynamic polymorphism, then we would define the add function to take some kind of "IAddable" object as its arguments. Any object that implement that interface (or derive from that base class) can be used despite their different implementations, which gives us the polymorphic behavior. We don't care which type is passed to us, as long as it implements some kind of "can be added together" interface. However, the compiler doesn't actually know which type is passed to the function. The exact type is only known at runtime, hence this is dynamic polymorphism.

Here, though, we don't require you to derive from anything, the type T just has to define the + operator. It is then inserted statically. So at compile-time, we can switch between any valid type as long as they behave the same (meaning that they define the members we need)

This is another form of polymorphism. In principle, the effect is the same: The function works with any implementation of the concept we're interested in. We don't care if the object we work on is a string, an int, a float or a complex number, as long as it implements the "can be added together" concept.

Since the type used is known statically (at compile-time), this is known as static polymorphism. And the way static polymorphism is achieved is through templates and function overloading.

However, when a C++ programmer just say polymorphism, they generally refer to dynamic/runtime polymorphism.

(Note that this isn't necessarily true for all languages. A functional programmer will typically mean something like static polymorphism when he uses the term -- the ability to define generic functions using some kind of parametrized types, similar to templates)

jalf
+2  A: 

When a C++ programmer says "polymorphism" he most likely means subtype polymorphism, which means "late binding" or "dynamic binding" with virtual functions. Function overloading and generic programming are both instances of polymorphism and they do involve static binding at compile time, so they can be referred to collectively as compile-time polymorphism. Subtype polymorphism is almost always referred to as just polymorphism, but the term could also refer to all of the above.

Eric Mickelsen
+2  A: 

"Polymorphism" literally means "many forms". The term is unfortunately a bit overloaded in computer science (excuse the pun).

According to FOLDOC, polymorphism is "a concept first identified by Christopher Strachey (1967) and developed by Hindley and Milner, allowing types such as list of anything."

In general, it's "a programming language feature that allows values of different data types to be handled using a uniform interface", to quote Wikipedia, which goes on to describe two main types of polymorphism:

Parametric polymorphism is when the same code can be applied to multiple data types. Most people in the object-oriented programming community refer to this as "generic programming" rather than polymorphism. Generics (and to some extent templates) fit into this category.

Ad-hoc polymorphism is when different code is used for different data-types. Overloading falls into this category, as does overriding. This is what people in the object-oriented community are generally referring to when they say "polymorphism". (and in fact, many mean overriding, not overloading, when they use the term "polymorphism")

For ad-hoc polymorphism there's also the question of whether the resolution of implementation code happens at run-time (dynamic) or compile-time (static). Method overloading is generally static, and method overriding is dynamic. This is where the terms static/compile-time polymorphism and dynamic/run-time polymorphism come from.

Laurence Gonsalves