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:
Bird
, or the code will not compile. If anyone wants to use my function, they must be aware that I only accept Bird
sobjects
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.