• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

interface implementation and inner classes

 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have see two implementations of a MouseListener interface and I am a little confused. In the first one I add "implements" to the class and implement the methods.



In the second example (shown below) uses an inner class to implement the MouseListener. Both examples seem to work fine, but I am confused by the use of "new MouseListener()", since it is an interface, not a class.

 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search for "anonymous classes".
 
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I much prefer the second solution, which Rob has told you is called an anonymous class. You can see all sorts of examples where the parent Component implements an interface and the fooPerformed(FooListener f) method is full ofIf you add Listener objects to the individual Components, you can get direct communication between related objects.
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob and Campbell, the clues help me find this article on using the anonymous classes to implement interfaces.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Larry, I just happened to ask this same question a week or so ago here:

https://coderanch.com/t/416796/java/java/Event-handling-Implementing-ActionListener-vs

Hope that's useful.
 
Campbell Ritchie
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. Those last two posts are exactly what we want to see; you give people a hint and they go and find out more information for themselves.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic