• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overidden method from JQ5Plus exam

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .....
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think page 168 in K&B's book explains it.

It's under the heading "Constructor Chaining".
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
https://coderanch.com/t/260857/java-programmer-SCJP/certification/Inheritance-Overriding-Doubts


Yours,
Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic