What’s an example of duck typing in Java?

Java is by design not fit for duck typing. The way you might choose to do it is reflection: public void doSomething(Object obj) throws Exception { obj.getClass().getMethod(“getName”, new Class<?>[] {}).invoke(obj); } But I would advocate doing it in a dynamic language, such as Groovy, where it makes more sense: class Duck { quack() { println … Read more