This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implementing an interface in anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implementing an interface in anonymous class" Watch "Implementing an interface in anonymous class" New topic
Author

Implementing an interface in anonymous class

Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
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.
Alex Collins
Greenhorn

Joined: Mar 29, 2004
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing an interface in anonymous class
 
Similar Threads
Mock Blues 1
Anonymous Inner Class from K&B
Java Basics
Inner classes must extend?
Diff bw Abstract class and Interface