wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes anonymous inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous inner class question" Watch "anonymous inner class question" New topic
Author

anonymous inner class question

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
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?

Thanks.


SCJA, SCJP5.0, SCBCD, SCWCD
Maciek Makowski
Greenhorn

Joined: May 17, 2005
Posts: 11
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: anonymous inner class question