[programming-languages] What is duck typing?

Consider you are designing a simple function, which gets an object of type Bird and calls its walk() method. There are two approaches you can think of:

  1. This is my function, and I must be sure that it only accepts the Bird, or the code will not compile. If anyone wants to use my function, they must be aware that I only accept Birds
  2. My function gets any objects and I just call the object's walk() method. So, if the object can walk() it is correct, if it can't my function will fail. So here it is not important the object is a Bird or anything else, it is important that it can walk() (This is duck typing)

It must be considered that the duck typing may be useful in some cases, for example Python uses duck typing a lot.


Useful reading