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.
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 ]
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 .....
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!
Enthuware Software Support
posted 13 years ago
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.
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