• 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 class implementing Interface

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Here's a quote from javacaps' self review on Objective 6:
9. Which of the following statements are true?
[a] Inner classes cannot be defined private
[b] An anonymous class can be subclass of another explicit class
[c] An anonymous class can implement single interface
[d] An anonymous class cannot be both explicit subclass and implement an interface
My initial answer would be (b).
But, the javacaps' answer is (b), (c), (d)
I'm a bit confused here.. How can one anonymous class implement an interface? And what does option (d) really mean?
Please consider the following code:

The current code compiles & runs successfully (& as I expected). Now, the question is how to declare an anonymous class which implements Z, for example. And how the (d) option illustrates itself within the above code?
please anyone enlightened me...
thanks in advance
- eric
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because of the way Anonymous classes are declared. Recall that Anonymous classes are declared inside methods with a special shorthand.
new Thread(){
public void run(){ .....}
}
Extends a class, while the following implcitly extends Object and implements an interface:
new Runnable(){
public void run() {..}
}
So there is no way this syntax could both extend a class and implement an interface.
Bill
------------------
author of:
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
Thanks for your explanation.
Now, can I come into a conclusion that even though an interface cannot be instantiated, it can still though be implemented in an anonymous class?
The point is when we declare an anonymous class to be:
a) new SomeClass() { }
it will be considered as a subclass of SomeClass
b) new SomeInterface() { }
it will be considered as a subclass of java.lang.Object and implements SomeInterface
but, there can never be an inner class which is a subclass of an explicit class (that is, other than java.lang.Object) and implements an interface at the same time, right?
please correct me if I'm wrong
thanks again, Bill
- eric
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
I tried this two codes and found the same error.
show() in Super.Sub cannot implement show() in Super.InFace; attempting to assign weaker access privileges; was public


So can we decide that Non-static inner classes can't explicitely implement interfaces? But It should, i think.
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 06, 2001).]
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Azaman,
I think your only problem was you implement the show() method incorrectly. I've tried and declare show() method in Sub class as public instead, and it's working correctly for both scenarios. So, I guess non-static inner class can still extends an explicit class and implements an interface.
- eric

 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Eric,
I forgot when coding that methods in interfaces are implicitely public when overriding them i should have given public access specifier.
Thnx to pointing out the problem....
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 14, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic