Main {{ public static void p(String s){ System.out.print(s); } public static void main(String args[]) throws Exception { Start start = Start.parse(System.in); // some sample functionality p(start.display() + "\n"); p(start.print() + "\n"); p(start.toStr() + "\n"); System.out.println(start.equals(start)); p(start.magic().display() + "\n"); p(start.bottomUp() + "\n"); } }} Start {{ // magically replace apples by oranges and vice versa // and keep the rest of the structure the same public Start magic() { return new Traversal(new Bc(){ Orange combine(Apple a){return new Orange();} Apple combine(Orange o){return new Apple();} }). traverse(this); } }} Start {{ public int bottomUp() { return new Traversal(new ID() { // add structure details gradually int combine(Apple a){return 4;} // int combine(Pencil p){return 10;} int combine(Item p){return 10;} // bottomUp jumps from 20 to 73 int combine(Empty e){return 0;} int combine(Cons o, int n1, int n2){return n1+n2;} // int combine(Object o, int n1){return n1;} int combine(Start o, int n1){return n1;} int combine(Basket o, int n1){return n1;} // interplay between structure-shyness and safety int combine(Fruit a){return 5;} }). traverse(this); } }}