• 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

Inner class/anonymous

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got this mock question:
Which statement is true?
A)An anonymous inner class can implement multiple interfaces
b)An anonymous inner class may be declared as final
c)An anonymous inner class can be declared as private
d)Construction of an instance of a static inner class requires an instance of the enclosing outer class
e)An anonymous inner class can access final variables in any enclosing scope
The correct answer is E.
I got this question right, purely because I knew that E was right. However I wasn't sure about the others. Can someone go through each of the other answers and explain why they are not correct.
Any help would be much appreciated.
Rowan.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rowan Chattaway:
Got this mock question:
Which statement is true?
A)An anonymous inner class can implement multiple interfaces


Well that's not true. An anonymous inner class can either:
1) Declare it implements ONE interface; the inner class then extends Object implicitly, and implements the declared interface. OR
2) Declare it subclasses another class.
Some examples of the first case when adding listeners:
myComponent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){}
});
This anonymous inner class declares that it implements the ActionListener interface. It is implictly a direct subclass of Object. You can only have ONE interface after the new, so there is no way to implement multiple interfaces.


b)An anonymous inner class may be declared as final


Since an anonymous class doesn't have a name, where exactly are you supposed to "attach" the final keyword? Besides, since it's anonymous, there's no way to access the class to subclass it anyway, so it is de facto final. But in any case, you can't include any modifiers when declaring the anonymous inner class.


c)An anonymous inner class can be declared as private


This is false for the same reasons I stated above. There is no named class where you can "attach" a private keyword. This wouldn't work:
myComponent.addActionListener(private new ActionListener(){}); //compiler error


d)Construction of an instance of a static inner class requires an instance of the enclosing outer class


Since it's static, it has no enclosing object context. That is, it exists independently of the existance of an enclosing object. Interfaces can't be inner, so there is no such requirement that you have an instance of an enclosing class to use it. Think about regular static members.
class Foo{ static int bar; }
I can access the static bar variable by simply writing Foo.bar. I have not created any actual instance of a Foo object.
[ March 14, 2002: Message edited by: Rob Ross ]
[ March 14, 2002: Message edited by: Rob Ross ]
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Rob, that was an excellent answer - really cleared things up!
Why haven't you taken your exam yet? You'd almost definately get a superb result, possibly 100%!
 
reply
    Bookmark Topic Watch Topic
  • New Topic