This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

interface implementation and inner classes  RSS feed

 
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
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for "anonymous classes".
 
Campbell Ritchie
Sheriff
Posts: 53734
127
  • 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
Posts: 53734
127
  • 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
Boost this thread!