//: staticchecking/latent/Latent.java // ©2015 MindView LLC: see Copyright.txt package staticchecking.latent; import java.lang.reflect.*; class Dog { public void talk() { System.out.println("Woof!"); } public void reproduce() {} } class Robot { public void talk() { System.out.println("Click!"); } public void oilChange() {} } class Mime { public void walkAgainstTheWind() {} public String toString() { return "Mime"; } } class Communicate { public static void speak(Object speaker) { try { Class spkr = speaker.getClass(); Method talk = spkr.getMethod("talk", (Class[])null); talk.invoke(speaker, new Object[]{}); } catch(NoSuchMethodException e) { System.err.println( speaker + " cannot talk"); } catch(IllegalAccessException e) { System.err.println( speaker + " IllegalAccessException"); } catch(InvocationTargetException e) { System.err.println( speaker + " InvocationTargetException"); } } } public class Latent { public static void main(String[] args) { Communicate.speak(new Dog()); Communicate.speak(new Robot()); Communicate.speak(new Mime()); } } ///:~