aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous classes" Watch "Anonymous classes" New topic
Author

Anonymous classes

Michael Carlson
Ranch Hand

Joined: Sep 11, 2005
Posts: 78
I've read that anonymous classes can implement an interface, I've also read that the anonymous class declaration can't have an implements clause, could someone give me an example of the correct way.

interface I{
void aMethod();
}
class A{


}
public class B{

A a = new A() implements I{
void aMethod(){}
}
}
The compiler complains.
Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Your guess was really close. You don't need an "implements".But you do need a semicolon after the assignment statement. I find it hard to get the various punctuation in the right place, so when I'm typing this I start like this:and then I insert the required methods.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In the example below, the variable i is of type Inter (which is an interface). It might look like we're trying to instantiate an interface by calling new, but we're really instantiating an anonymous class that implements Inter. The resulting reference is automatically upcast to the interface type.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Michael Carlson
Ranch Hand

Joined: Sep 11, 2005
Posts: 78
I got it now, thanks for the examples.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Paul Clapham:
Your guess was really close. You don't need an "implements"....

Unless A is implementing I, this isn't really implementing the interface. It's just extending A by adding a new method that won't be reachable after the automatic upcast to type A.
Michael Carlson
Ranch Hand

Joined: Sep 11, 2005
Posts: 78
So marc, your saying the anonymous sub class of A is not implementing the interface, therefore the super class must implement the interface, and the anonymous class extends the super class. In your example your using an implementer of the interface, rather than a subclass of the class.
Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Michael Carlson:
So marc, your saying the anonymous sub class of A is not implementing the interface, therefore the super class must implement the interface, and the anonymous class extends the super class. In your example your using an implementer of the interface, rather than a subclass of the class.
Thanks

Right. An anonymous class either extends a class (without using "extends") or implements a single interface (without using "implements")...

ClassOrInterface x = new ClassOrInterface() { //anonymous body };

The reference is automatically upcast to the type of the extended class or the implemented interface. This is why you can only override or implement methods that are already declared in the superclass or interface. Any new methods you add to the anonymous body won't be accessible after the upcast.
[ February 06, 2006: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous classes