Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous inner class question

 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maciek Makowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic