aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overidden method from JQ5Plus exam 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 "Overidden method from JQ5Plus exam" Watch "Overidden method from JQ5Plus exam" New topic
Author

Overidden method from JQ5Plus exam

Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342


output:
A constructor
B version: 0
B constructor
B version: 4

why it print out 0 not "A" (B constructor not even run yet)??
[ December 14, 2006: Message edited by: Anthony Karta ]

SCJP 5
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
Hi Anthony,
I am trying to locate this question in our questionbank to see if we haven't provided sufficient explanation with it, but I'm not able to find it.
Would it be possible for you to mention the questionid of the question?
thanks,
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Hi Paul,

I haven't look at the explanation, I'm not finished it yet.

I set "show the answer" to enable but it doesn't show correct answer/explanation for each question.

btw, Here is the id com.enthuware.ets.scjp.v5.2.442

thanks
JQPlus is great!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
To the problem:
print() in A becomes overridden when subclassed by B. So when A's constructor executes B's print() gets called. Notice that B and A may not be fully initialized when this occurs. So if you have a method in A which must be called by A's constructor then make it private so that subclasses cannot foul things up.
[ December 14, 2006: 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.
Vishanth Sundararajan
Greenhorn

Joined: Nov 02, 2006
Posts: 1
I cannot comprehend how version:0 is getting printed....
and is it not because print method in A's constructor is being accessed by the (this)i.e. the object of B .so anyway even if we declare it as private only B's version of the print method will be called .....
Arpit Khandelwal
Greenhorn

Joined: Dec 11, 2006
Posts: 10
I cannot comprehend how version:0 is getting printed....
and is it not because print method in A's constructor is being accessed by the (this)i.e. the object of B


"Keep it simple" is the secret to ones success.

Well here we should always keep in mind that the object being referenced is B not A.

So in A's constructor the call to print will in fact be B.print(compiler will first search the print in B and if not found there the o/p will be A instead) as we have specified A a = new B();

Hope this satisfies you Vishanth.


-Arpit Khandelwal<br />"impossible" itself says i m possible!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
Thanks, Anthony. I got it.

Note that method print() is overridden in class B. Due to polymorphism, the method to be executed is selected depending on the class of the actual object.
Here, when an object of class B is created, first A's constructor is called, which in turn calls print(). Now, since the class of actual object is B, B's print() is selected. At this point of time, variable i has not been initialized (because we are still initializing A at this point), so its default value i.e. 0 is printed.
This happens because the method print() is non-private, hence polymorphic.

Finally, 4 is printed.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
Originally posted by Anthony Karta:
I set "show the answer" to enable but it doesn't show correct answer/explanation for each question.


This option is effective only when you are viewing a question outside of any test. For example, if you search for a particular question and open it, depending on this option, you can either view the answer immediately or you can attempt the question, get it evaluated (i.e. after selecting options, you must click "Evaluate" to see the correct answer).

While taking a test, answers are shown only after the test is finished.

If you are seeing any other behavior, please let us know.


JQPlus is great!


Thanks a lot for the appreciation. It goes a long way in motivating us to improve it constantly
Wes Misenheimer
Greenhorn

Joined: May 21, 2006
Posts: 11
I think page 168 in K&B's book explains it.

It's under the heading "Constructor Chaining".
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Anthony asked

why it print out 0 not "A" (B constructor not even run yet)??


to be more precise: not run to the end.


Me and Jothi answered almost the same question yesterday:
http://www.coderanch.com/t/260857/java-programmer-SCJP/certification/Inheritance-Overriding-Doubts


Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overidden method from JQ5Plus exam