This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Implementing an interface in anonymous class  RSS feed

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through some of the excellent mock exams on the Dan Chisholm site and came across a question I need to see some code to understand. The answer indicates that while anonymous classes can not use the implements keyword (which I can understand), they can implement interfaces. Can someone provide a simple example on how you can implement an interface without specifying the interface you are implementing and why you would do so? The only thing I can think of is something like the following (albeit a bit contrived):

In this case, the anonymous class is not using the implements keyword but is implementing the Runnable interface by specifying a method body to the run method.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best example is when using Gui's, and when creating a button (for example) supplying the implementation of it's ActionListener in an anonymous class using the ActionListener interface from java.awt.
Study this code:
----------------------------


Do you understand?
Using the ActionPerformed interface in the addActionListener of the button component, i created an anonymous class (implementing the ActionListener interface) to System.out "Button Clicked" and a number each time.

I hope that helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!