Inner Class Question

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.

:|
 
V

VisionSet

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. ....

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.

It is basically asking which statements create valid annoymous classes, and
no more.
The fact it is from within class Bar() seems irrelvant in this case.
There is nothing in the question that requires a Bar instance to be created.

C is correct because it creates a valid annoymouse class, a class that
implicitly extends Boo and has its own attribute of String s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top