• 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 inner class that extends a super and implements an interface

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following statement is gived as a correct answer in a question: "Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface." , but how to declare this kind of anonymous class that extends a super class and also implements a single interface? any help, thanks!
[ October 28, 2002: Message edited by: Li Wenfeng ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the question?
 
Li Wenfeng
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry!
here it is!
which two are true?
A. An anonymous inner class can be declared inside of a method
B. An anonymous inner class constructor can take arguments in some situations
C. An anonymous inner class that is a direct subclass of Object can implements
multiple interface
D. Even if a class Super does not implement any interfaces, it is still possible
to define an anonymous inner class that is an immediate subclass of Super that
implements a single interface
E. Even if a class Super does not implement any interfaces, it is still possible
to define an anonymous inner class that is an immediate subclass of Super that
implements multipe interface
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This answer is wrong, I guess they didn't mean Super class but Enclosing class. In this case you can implement an interface in the anonymous class even if this interface is not implemented but the enclosing class...
or am I missing something??
 
Li Wenfeng
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is a,b. In Option b, It may like this:
class Super{
Super(int i){}
}
class Base{
void createAnonymous(){
new Super(5){};
}
}
Am i right?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Li Wenfeng
You are correct,
The anonymous inner class either extends a class or implements an interface, I don't think we can do both at the same time.
Mallik
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The anonymous inner class either extends a class or implements an interface, I don't think we can do both at the same time.
That's correct.
Moreover, note that:


if an anonymous class is derived from an interface I, the actual superclass is Object, and the class implements I rather than extending it. (Explicit implements clauses are illegal.) This is the only way an interface name can legally follow the keyword new. In such cases, the argument list must always be null, to match the constructor of the actual superclass, Object.


(From the Java Inner classes specification)
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think the answer is a,b. In Option b, It may like this:
class Super{
Super(int i){}
}
class Base{
void createAnonymous(){
new Super(5){};
}
}
Am i right?


Q1. Anonymous class can not have constructor as they do not have names. So how is option b correct?
Q2. Anonymous classes are defined and instanciated in a context where a reference can be used. Would it make more sense to change the code a bit...

Thanks
Barkat
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic