• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous classes

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now, thanks for the examples.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic