0

Could someone present duck typing in java? I sort of understand the idea behind duck typing. If it quacks like a duck and walks like a duck, its a duck regardless of what class it is.

So how can this concept be presented in java?

Thank you!

Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • 2
    As far as I know there is no such thing as ducktyping in Java. Duck typing implies that a "type" of an object is determined by the fields and/or methods they make public. But, since every method in Java has a type signature (return type, parameter types), there is no way for you to feed it any object you like, because you know it has the properties it needs. This is enforced by the typesystem at compile time. You can not declare a variable without a type. – Christophe De Troyer Apr 24 '14 at 20:12
  • You can perform "duck typing" with reflection. [Here](http://www.frischcode.com/2013/11/clean-up-after-yourself.html) is an example. – Elliott Frisch Apr 24 '14 at 20:15
  • See http://stackoverflow.com/questions/1079785/whats-an-example-of-duck-typing-in-java – vboerchers Apr 24 '14 at 20:20

0 Answers0