fork download
  1. import java.lang.reflect.Method;
  2. class Ideone {
  3. public static void main (String[] args) throws Exception {
  4. Foo foo = new Bar();
  5.  
  6. // cast to type
  7. ((Bar)foo).start("via casting");
  8.  
  9. // cast to interface
  10. ((Startable)foo).start("via casting2");
  11.  
  12. // reflection
  13. Method m = foo.getClass().getDeclaredMethod("start", String.class);
  14. m.invoke(foo, "via reflection");
  15. }
  16.  
  17. public interface Startable {
  18. void start(String arg);
  19. }
  20. public static class Foo {
  21. }
  22.  
  23. public static class Bar extends Foo implements Startable {
  24. public void start(String arg) {
  25. System.out.println("Hello World: " + arg);
  26. }
  27. }
  28. }
Success #stdin #stdout 0.12s 35748KB
stdin
Standard input is empty
stdout
Hello World: via casting
Hello World: via casting2
Hello World: via reflection