Duck typing and polymorphism

Often duck typing is assimilated to polymorphism, since it allows in some way to uniformly manage different type of objects. However, they are different concepts.

Polymorphism is a concept found on types whereas duck typing is found on contracts. With polymorphism, it is important what an object is and not how it behaves. In duck typing, it is important how an object behaves. Duck typing is more tied to the concept of objects that interact, rather than objects that are of a certain type.

However, duck typing can help to implement solutions that strongly typed languages resolve with true polymorphism. Let's consider an example where our software house wants to implement a method that creates a list of full names of all ...

Get Mastering JavaScript Object-Oriented Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.