File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anno. Class Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anno. Class Question" Watch "Anno. Class Question" New topic

Anno. Class Question

Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
Question extracted from MasterExam from KB book.

The answer suggested that "Bing b = new Bing(){"Foo";};" is one of the correct answers. But it does not compile for me. Can someone explain the logic behind this ?

Second, "Bang b = new Bing("Foo"){}; " is wrong because I think it's syntatically incorrect. The suggested answer saying that "incorrect because it violates the rules of polymorphism-you cannot refer to the super class type using a reference variable declared as a subclass type. The super class is not quaranteed to have everything the subclass has. " Especially the last sentence, it completely throw me off. Again, can someone explain the logic for me? Personally, i think it should not even mention about polymorphism at all.

By the way, are the questions in the MasterExam difficulty equivalent to the real exam ?

Thanks in advance,

Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you checked the errata?

Sorry but I don't know the page that question is on. "Bing b = new Bing(){"Foo";};" is definitely wrong.

Bing b = new Bing("Foo"){}; is OK it means that b is a reference to an object that is an instance of an anonymous class derived from Bing. Notice that the constructor Bing(String s) will be used to construct the new object.

Bang b = new Bing("Foo"){}; is not OK because the new anonymous class is a Bing not a Bang.
[ October 09, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112

Thank you for the prompt response. I couldn't "search" any related erata in the URL. The question was came out from the MasterExam CD of Kate and Bates book.

Here is the original question.

Basically the question as, Choose 2 are correctly applicable to the //comment line.

A) Bang b = new Bing("Foo"){};
B) Bing b = new Bing(343){};
C) Bing b = new Bing(){"Foo" ;
D) Bing b = new Bang(){};
E) Bing b = new Bing.Bang("Foo"){};

Which 2 would you choose ? Well, D definitely the right one.

Gurpreet Singh
Ranch Hand

Joined: Oct 05, 2004
Posts: 34
hey, Kay do you know the reason why

Bing b = new Bing(){"Foo";};

is incorrect. I guess you would have guessed the answer by now.
In case you haven't, "Foo" is not passed to any String variable or
to any variable at all.

Got it.
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112

Yeah, I got it now. After i posted this question, I did about 10-13 trials and errors to find out all possible can and can't combination.
I agree. Here's the link:
subject: Anno. Class Question
It's not a secret anymore!