Given the following,
public class Foo {
Foo() {System.out.print("foo");}
class Bar{
Bar() {System.out.print("bar");}
public void go() {System.out.print("hi");}
}
public static void main (
String [] args) {
Foo f = new Foo();
f.makeBar();
}
void makeBar() {
(new Bar() {}).go();
}
}
what is the result?
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi
E. hi
answer
-------
8. C is correct because first the Foo instance is created, which means the Foo constructor runs and prints �foo�. Next, the makeBar() method is invoked which creates a Bar
,which means the Bar constructor runs and prints �bar�, and finally the go() method is invoked on the new Bar instance
, which means the go() method prints �hi�.
A, C, D, E, and F are incorrect based on the program logic described above.
what my doubt is in method makeBar(){
(new Bar(){}/annoynomous class).go();
but bert is saying creates Bar and calls go method on Bar instace in the explanation .what i thing is the created object is subtype of Bar since annoymous class is invovled.please tell whether i am right or not