fork download
  1. import java.lang.*;
  2.  
  3. class Main {
  4. public static void main(String[] argv) {
  5. SandwichMaker<Cheese> sandwich = new CheeseSandwichMaker();
  6. sandwich.make(new Cheese("Camembert", "Normandy, France"));
  7. sandwich.make(new Cheese("Cheddar", "Somerset, England"));
  8. }
  9. }
  10.  
  11.  
  12. abstract class SandwichMaker<S extends Spread> {
  13. public void make(S spread) {
  14. toastBread();
  15. addSpread(spread);
  16. enjoy();
  17. }
  18.  
  19. protected void toastBread() {
  20. System.out.println("...toasting bread");
  21. }
  22.  
  23. protected abstract void addSpread(S spread);
  24.  
  25. protected void enjoy() {
  26. System.out.println("this is yummy!");
  27. }
  28. }
  29.  
  30. class CheeseSandwichMaker extends SandwichMaker<Cheese> {
  31. @Override
  32. protected void addSpread(Cheese cheese) {
  33. System.out.println("... adding " + cheese.name + " cheese from " + cheese.origin);
  34. }
  35. }
  36.  
  37. interface Spread {}
  38. class Cheese implements Spread {
  39. public final String name;
  40. public final String origin;
  41.  
  42. public Cheese(String name, String origin) {
  43. this.name = name;
  44. this.origin = origin;
  45. }
  46. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
...toasting bread
... adding Camembert cheese from Normandy, France
this is yummy!
...toasting bread
... adding Cheddar cheese from Somerset, England
this is yummy!