Is duck typing a subset of polymorphism

Posted by Raynos on Programmers See other posts from Programmers or by Raynos
Published on 2011-11-25T22:44:16Z Indexed on 2011/11/26 2:06 UTC
Read the original article Hit count: 632

Filed under:
|
|

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

In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

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 ?

© Programmers or respective owner

Related posts about oo

Related posts about duck-typing