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

Instantiation of a Listener using an anonymous inner class

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can a method in an anonymous inner class ever be private? why or why not?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try out if you can add a private method to the anonymous inner class?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried changing it to a private scope, and an error comes up.

I don't understand why it can't be private..
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A child class needs to satisfy the super class contract. When you narrow down the scope, you break the conract
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The actionPerformed method cannot be made private, because it's a method that's inherited from the interface that the anonymous class is inheriting. But this, by itself, has nothing to do with the fact that it's an anonymous inner class. Methods declared in interfaces are always public, so in a class that implements an interface (no metter if it's an anonymous inner class or a regular class), the methods inherited from the interface must always be public.

But you can add private methods if you like, for example:
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a different listener for each colour. That block of ifs is in my opinion not object‑oriented. Search my posts in the Swing forum for "ColourListener".

As for Swing forum, I think this discussion would sit better there, so I shall move it.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! that can't be any clearer!
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it right to say:
• The parameter ‘e’ can be renamed as long as its type is the same.
• The class that has the interface type InnerMenuListener (deduced from the constructor) is the anonymous class definition in the code.
• actionPerformed CANNOT be renamed because it is implementing an interface, therefore the names are prescribed by the interface.

I'm not too sure how to answer this question though?
What is referenced by ‘e’, the parameter of the actionPerformed(…) method?


Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic