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 ]