I'm sure I'm not the first to blog this but let's go: This simple example shows how we can have the Duck Typing effect using reflection on the Java platform:
1:import static java.lang.System.out;
2:
3:public class VerboseButStillDuckTyping
4:{
5: public static void main(String[] args) throws Exception
6: {
7: quack(new WoodDuck());
8: quack(new Object());
9: }
10:
11: static void quack(Object obj) throws Exception
12: {
13: Duck.class.getMethod("quack", null).invoke(obj, null);
14: }
15:}
16:
17:interface Duck { void quack(); }
18:
19:class WoodDuck implements Duck
20:{
21: public void quack() { out.println("Quaaack!"); }
22:}
2:
3:public class VerboseButStillDuckTyping
4:{
5: public static void main(String[] args) throws Exception
6: {
7: quack(new WoodDuck());
8: quack(new Object());
9: }
10:
11: static void quack(Object obj) throws Exception
12: {
13: Duck.class.getMethod("quack", null).invoke(obj, null);
14: }
15:}
16:
17:interface Duck { void quack(); }
18:
19:class WoodDuck implements Duck
20:{
21: public void quack() { out.println("Quaaack!"); }
22:}
Above we are invoking the quack method on two references, one that has the needed implementation and the other which does not have. As expected the second invocation raises an runtime error, as with any dynamic language :-)
Of course this is just a proof of the concept and not what should be a regular practice on development with the Java language. This is just to remember that people very often does not proper recognize the dynamic features of the Java runtime environment.


Comments
There is nothing "dynamic" in there. Introspection, perhaps. But introspection and dynamic typing are two complete different beasts.