File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overidden method from JQ5Plus exam

 
Anthony Karta
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3665
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anthony Karta
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vishanth Sundararajan
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3665
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3665
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think page 168 in K&B's book explains it.

It's under the heading "Constructor Chaining".
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic