• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instantiation of a Listener using an anonymous inner class

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can a method in an anonymous inner class ever be private? why or why not?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic