In K&B book, it says, 'because of polymorphism, the only methods you can call on an anonymous inner class reference are those defined in the reference variable class (or interface), even though the anonymous class is really a subclass or implementer of the reference variable type.'
Does this mean we CAN NOT or SHOULD NOT define new methods/variables in anonymous inner class since we cannot call any non-overriden methods in it?
SCJA, SCJP5.0, SCBCD, SCWCD
posted 11 years ago
You can define new methods in anonymous classes, but it does not make much sense for the reason you have given -- there are no easy means to access them from the outside. The only way I can think of is through reflection.
You may consider creating new methods so that your code is structured better, but this is more related to coding practices and is somewhat questionable, since you normally wouldn't want to have too much logic in an anonymous class. [ May 25, 2005: Message edited by: Maciek Makowski ]