fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public interface Fruit {
  11. public void apple();
  12. }
  13.  
  14. Fruit interfaceFruit;
  15.  
  16. public void apple(){
  17. System.out.println("apple");
  18. if (interfaceFruit != null) {
  19. interfaceFruit.apple();
  20. }
  21. }
  22.  
  23. public void setFruit(Fruit f) {
  24. interfaceFruit = f;
  25. }
  26.  
  27. public static void main (String[] args) throws java.lang.Exception
  28. {
  29. Ideone test = new Ideone(){
  30. @Override
  31. public void apple() {
  32. System.out.println("local override of apple");
  33. }
  34. };
  35. System.out.println("1) ---");
  36. test.apple();
  37.  
  38.  
  39. Ideone test2 = new Ideone();
  40. System.out.println("2) ---");
  41. test2.apple();
  42.  
  43. test2.setFruit(new Fruit() {
  44. @Override
  45. public void apple() {
  46. System.out.println("interface override of apple");
  47. }
  48. });
  49. System.out.println("3) ---");
  50. test2.apple();
  51. }
  52. }
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
1) ---
local override of apple
2) ---
apple
3) ---
apple
interface override of apple