Hi,
Yesterday, I took the mock
test included in the CD with Kathy and Bert's book. I came across a question which tested my knowledge of anonymous inner classes.
The question was as follows (The words might not be exactly similar here):
___________________________________________________________________________
Given the following code definition, which two declarations of anonymous inner classes inerted at line 1, will compile successfully?
class Bing{
Bing(
String s){
}
Bing(){
}
}
class Bang extends Bing{
Bang(String s){
super(s);
}
Bang(){
super();
}
// Line 1
}
___________________________________________________________________________
There were 2 correct answers to this question which are as follows:
1) Bing b = new Bang(){}
2) Bing b = new Bing(){"Foo";}
I have no problem with option 1 but how is option 2 correct here? I have tried inserting option 2 at line 1 and compiling the code but it doesn't compile and I am sure that this is not a correct syntax of instance initializer as well.
Has anybody else come across this question? Is the question wrong or if the option 2 is still correct, Please let me know how?
Thanks,
Nidhi.