Take the 2-minute tour ×

From Polymorphism on WIkipedia

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.

From duck typing on Wikipedia


My interpretation is that based on duck typing, the objects methods/properties determine the valid semantics. Meaning that the objects current shape determines the interface it upholds.

From polymorphism you can say a function is polymorphic if it accepts multiple different data types as long as they uphold an interface.

So if a function can duck type, it can accept multiple different data types and operate on them as long as those data types have the correct methods/properties and thus uphold the interface.

(Usage of the term interface is meant not as a code construct but more as a descriptive, documenting construct)

  • What is the correct relationship between ducktyping and polymorphism ?
  • If a language can duck type, does it mean it can do polymorphism ?
share|improve this question
Not sure what you are looking for in an answer. You defined both accurately, so you have as much of a definitive answer as there is. Deciding if duck typing "counts" as polymorphism is more or less a philosophical question, or maybe a terminology question if you mean it that way. So what would you be looking for in an answer? –  psr Nov 25 '11 at 23:11
@psr I was basically saying "here's how I interpret it. Am I wrong? Am I right? Are there repubable academic people that say it it one way or the other. Are there any articles on the topic?" Basically in detail other then "duck typing is an implementation / subset of polymorphism" what else can be said about the relationship? –  Raynos Nov 25 '11 at 23:15
as far as I understand homography makes duck typing anything else but uniform interface => not a polymorphism, or at least not in a sense of how Wikipedia defines it. Eg door.close() and tiger.close() –  gnat Nov 25 '11 at 23:19
Duck typing is a case of Ad-hoc polymorphism. You're over-thinking this. –  Yannis Rizos Nov 26 '11 at 22:20
add comment

5 Answers

I think that:

Both duck typing and polymorphism are means of indirection/abstraction. Polymorphism is a concept founded on types and typing whereas duck typing is founded on contracts.

With polymorphism it is important what the THING is and not how it behaves (it behavior may be the consequence of what it is).

In duck typing it is important how a THING behaves. Duck typing is more tied to the concept of objects as actors that exchange messages, rather than objects which have some defined properties.

share|improve this answer
add comment

I say that polymorphism is a generic trait, that can be implemented several ways:

  • class based inheritance.
  • prototype based objects (with or without inheritance)
  • duck typing
  • interface compliance (as done by Go's interfaces and implicitly on C++ templates)

each of them allows the programmer to use a single code with different types, so all embody the concept of polymorphism.

share|improve this answer
add comment

The answer is YES.

Duck Typing is a special case of dynamic typing, and even if dynamic typing per se cannot be directly considered as polymorphism (since it is just the property of the language to perform type check at runtime rather then at compile time), the techniques that usually underlie the dynamic typing, such late binding and dynamic dispatch are characteristic for polymorphism.

share|improve this answer
add comment

Duck typing is not quite a subset of polymorphism, since polymorphism requires explicitness, which is lost in duck typing. So duck typing has the chance of "implementing" an "interface" not because it makes semantic sense but because you have the same signature. If duck typing were a subset of polymorphism then it would have all the properties of polymorphism, including explicit declaration.

Duck typing also isn't really an implementation of polymorphism, it's part of a different type system. Usually you associate duck typing with a dynamic language that goes ahead and passes a message to an object without knowing whether the object can handle it - if it can then it has effectively passed the duck typing test. Usually you think of polymorphism as being implemented at compile time by setting up vtables (and itables if interfaces are different than classes). But there are LOTS of languages out there and lots of ways to implement these features.

To some extent this is a philosophical question. Can you think of duck typing as interfaces that are automatically declared implicitly? I can't think of any reason that's wrong, exactly, but I don't think it's probably the most productive way to look at it. I think duck typing and interfaces are both commonly encountered features of programming language's type systems, both have fairly similar ways of behaving, and both are important for programmers to understand.

share|improve this answer
"polymorphism requires explicitness" where does it say so? class-based inheritance is just a form of polymorphism, not the only one. –  Javier Nov 26 '11 at 11:17
And continuing @Javier's comment, how about ad hoc polymorphism? –  Yannis Rizos Nov 26 '11 at 22:12
@YannisRizos - The problem with answering any question like this is there are so many languages and so much terminology that someone might nitpick you no matter what you say. I understand why "ad hoc polymorphism" has the word "polymorphism" in the name, but I would argue it's something else than what the OP means by "polymorphism". –  psr Nov 28 '11 at 15:59
@Javier - All the methods you listed require explicitness except duck typing. –  psr Nov 28 '11 at 16:04
@psr Actually I don't care about what OP meant by "polymorphism". I don't think anyone should care, it's a scientific concept with a very simple definition, I don't think we should answer based on our own interpretations or what we may think it's the OPs interpretation. Especially the OPs interpretation, since he is asking so by default he is unsure about his interpretation. I disagree that's it's a philosophical question, the question as stated was a scientific one with one simple answer: Yes ducktyping is a form of ad hoc polymorphism, one subset of polymorphism. –  Yannis Rizos Nov 28 '11 at 21:14
show 1 more comment

It feels almost natural to say "Yes, if foo has duck typing, foo has polymorphism". But I can't say this 100% sure, in a sense that maybe it is possible to come up with an artificial examples of such system which has duck typing ("can it quack && can it float on water ==> it is a Duck") while not having polymorphism ("foo, quack!" fails), but they would really be artificial and in a real world, I'd say "Yes, if duck typing is present, polymorphism must also be present".

Personally I see duck typing as "polymorphism done right". What I mean by that, thing that exist in a duck typing world do not need to have any explicit types and their (polymorphic = "same access, different outcome") behaviour is the only thing that counts. In other implementations of polymorphism, it is constrained to types/interfaces/inheritance, so it is "implemented and constrained polymorphism" not a "polymorphism per se".

share|improve this answer
add comment

Your Answer


