Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface implementation and inner classes

 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • 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.

 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for "anonymous classes".
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry, I just happened to ask this same question a week or so ago here:

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

Hope that's useful.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic