/* This example shows how you can create anonymous classes that implement an interface. The example adds functions to a mythical calculator that has a method for adding functions to the calculator. */ class AnonymousExample { public static void main(String[] args) { MyCalc calc = new MyCalc(); calc.add( new UnaryFunction() { public double eval(double x) { return Math.sin(x); } public String toString() { return "sin(x)"; } } ); calc.add( new BinaryFunction() { public double eval(double x, double y) { if (x < y) return x; else return y; } public String toString() { return "min(x, y)"; } } ); } } /* Mythical calculator. Just enough to get the example above to compile. */ class MyCalc { void add(UnaryFunction f) {} void add(BinaryFunction f) {} }