This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous classes

 
Michael Carlson
Ranch Hand
Posts: 78
  • 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
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic