class Main{ void main(){ IVisitor v = new Visitor(); FooBar f = new FooBar(); f.accept(v); } } interface IVisitor{ void visit(Foo f); void visit(FooBar fb); } interface IAcceptor{ void accept(IVisitor v); } class Visitor implements IVisitor{ @Override public void visit(Foo f) { } @Override public void visit(FooBar fb) { } } class Foo implements IAcceptor{ @Override public void accept(IVisitor v) { v.visit(this); } } class FooBar implements IAcceptor{ @Override public void accept(IVisitor v) { v.visit(this); } }