It might
look like we're instantiating A. But actually, we're creating instances of anonymous types that are
subclasses of A. These subclasses are not abstract because they implement A's abstract method. The objects are then
upcast to type A.
When you create an anonymous inner class, you essentially call the constructor of an existing class -- but after the constructor's arguments (where you would normally have just a semicolon), you insert a new class definition in brackets. From this, you get an object that is a
subclass of the class whose constructor you called, and is
automatically upcast from the anonymous type to the class whose constructor you called.
In this case, we have objects of an anonymous type that extend class A, and are automatically upcast to type A. In the anonymous class definitions -- the parts in brackets slipped in between "new A(2,1)" and the semicolon -- we are providing implementation for the method, math(). So although the superclass is abstract, the anonymous subclasses are not.
See Chapter 8 of Bruce Eckel's
Thinking in Java (anonymous inner classes are discussed about halfway through)...
http://www.faqs.org/docs/think_java/TIJ310.htm NOTE: Alternatively, anonymous classes can implicitly
implement an interface instead of extending another class. The syntax is the same. Either way, the critical implementation is in the anonymous class body, and the resulting object is automatically upcast to the parent or interface type.
[ December 23, 2004: Message edited by: marc weber ]