File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous classes

 
Michael Carlson
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20158
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Carlson
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it now, thanks for the examples.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic