L
lonelyplanet999
Hi,
I am studying for 310-035 with book "Sun Certified Programmer &
Developer for Java 2" written by Kathy Sierra & Bert Bates.
I met one question which I don't understand the standard answer & its
explanation. Question below:
Given the following,
class Boo {
Boo(String s) { }
Boo() { }
}
class Bar extends Boo {
Bar() { }
Bar(String s) {super(s);}
void zoo() {
// insert code here
}
}
which two create an anonymous inner class from within class Bar?
(Choose two.)
A. Boo f = new Boo(24) { };
B. Boo f = new Bar() { };
C. Boo f = new Boo() {String s;};
D. Bar f = new Boo(String s) { };
E. Boo f = new Boo.Bar(String s) { };
The book says correct answers are B and C.
I understand why B is correct but don't understand why C is also
correct.
The book says B is correct because anonymous inner classes are no
different from any other class when it comes to polymorphism. That
means you are always allowed to declare a reference variable of the
superclass type and have that reference variable refer to an instance
of a subclass type, which in this case is an anonymous subclass of
Bar. Since Bar is a subclass of Boo, it all works. C uses correct
syntax for creating an instance of Boo.
However, C only results in an instance of Boo, not Bar plus an inner
class object be created. Also, an object of Boo is not equivalent to
an object of Bar.
:|
I am studying for 310-035 with book "Sun Certified Programmer &
Developer for Java 2" written by Kathy Sierra & Bert Bates.
I met one question which I don't understand the standard answer & its
explanation. Question below:
Given the following,
class Boo {
Boo(String s) { }
Boo() { }
}
class Bar extends Boo {
Bar() { }
Bar(String s) {super(s);}
void zoo() {
// insert code here
}
}
which two create an anonymous inner class from within class Bar?
(Choose two.)
A. Boo f = new Boo(24) { };
B. Boo f = new Bar() { };
C. Boo f = new Boo() {String s;};
D. Bar f = new Boo(String s) { };
E. Boo f = new Boo.Bar(String s) { };
The book says correct answers are B and C.
I understand why B is correct but don't understand why C is also
correct.
The book says B is correct because anonymous inner classes are no
different from any other class when it comes to polymorphism. That
means you are always allowed to declare a reference variable of the
superclass type and have that reference variable refer to an instance
of a subclass type, which in this case is an anonymous subclass of
Bar. Since Bar is a subclass of Boo, it all works. C uses correct
syntax for creating an instance of Boo.
However, C only results in an instance of Boo, not Bar plus an inner
class object be created. Also, an object of Boo is not equivalent to
an object of Bar.
:|