fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7.  
  8. class Parent {
  9. // those should be declared outside
  10. static interface Foo {
  11. void foo();
  12. }
  13. static interface Bar {
  14. void bar();
  15. }
  16.  
  17. protected String neededByFoo = "foo";
  18. protected String neededByBar = "bar";
  19.  
  20. class FooA implements Foo {
  21. public void foo() {
  22. System.out.println(neededByFoo + "A");
  23. }
  24. }
  25. class FooB implements Foo {
  26. public void foo() {
  27. System.out.println(neededByFoo + "B");
  28. }
  29. }
  30. class BarA implements Bar {
  31. public void bar() {
  32. System.out.println(neededByBar + "A");
  33. }
  34. }
  35. class BarB implements Bar {
  36. public void bar() {
  37. System.out.println(neededByBar + "B");
  38. }
  39. }
  40. public static interface FooBar extends Foo, Bar {}
  41. private static class FooBarDelegate implements FooBar {
  42. Foo fooDelegate;
  43. Bar barDelegate;
  44. private FooBarDelegate(Foo f, Bar b) { fooDelegate = f; barDelegate = b; }
  45. public void foo() { fooDelegate.foo(); }
  46. public void bar() { barDelegate.bar(); }
  47. }
  48.  
  49. public FooBar fooAbarA() {
  50. return new FooBarDelegate(new FooA(), new BarA());
  51. }
  52. public FooBar fooBbarA() {
  53. return new FooBarDelegate(new FooB(), new BarA());
  54. }
  55. public FooBar fooAbarB() {
  56. return new FooBarDelegate(new FooA(), new BarB());
  57. }
  58. public FooBar fooBbarB() {
  59. return new FooBarDelegate(new FooB(), new BarB());
  60. }
  61.  
  62. public static void main(String[] args) {
  63. Parent p = new Parent();
  64. foobar(p.fooAbarA());
  65. foobar(p.fooAbarB());
  66. foobar(p.fooBbarA());
  67. foobar(p.fooBbarB());
  68. }
  69. private static void foobar(FooBar fb) {
  70. fb.foo();
  71. fb.bar();
  72. }
  73. }
Success #stdin #stdout 0.1s 28084KB
stdin
Standard input is empty
stdout
fooA
barA
fooA
barB
fooB
barA
fooB
barB