Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Thu Sep 19, 2013 5:42 am
by TheSarjo
Hello!
I have a doubt about this exercise. I've compiled and run it, and it's as you have said, but...
I don't understand why the result is
Beta 44
4 44
and not
4 44
Beta 44

I mean, given the code:

Code: Select all

        Baap b = new Beta();         System.out.println(b.h + " " + b.getH());
The flow of execution should print in the System.out first b.h (which is 4, and THEN b.getH() wich has a new println, prints on a new line "Beta 44" and then.. 44 i don't know where it should go...
it doesn't concern with the question, but this "nested println" gets me confused...

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Thu Sep 19, 2013 7:57 am
by admin
Before the println is able to print the String, it has to form that String. So while it is building the String to print, it executes b.getH(). Now, this call goes to Beta's getH() because b is pointing to an object of class Beta. Before getH() returns a value of 44, it triggers a call to another println that prints "Beta 44".

Now, the original println can form the string with b.h+44, which is 4 44. (Here b.h refers to Baap's h) because b is of class Baap.

HTH,
Paul

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Mon Apr 06, 2015 12:43 pm
by ElizabethCM
Hi Paul,

Can you please briefly explain, line by line, how does this work?
I have some issues in understanding this.

This is the first line I have problems with:
System.out.println(b.h + " " + b.getH());

I understood from your explanations that in order to compute the whole parameter given in System.out.println() method, it needs to get the whole String, meaning to compute b.h + " " + b.getH(). It leaves this expression on the stack and computes b.getH().
It goes on the Beta.getH() method because the version that gets invoked on the overriden method is the one in the subclass. It prints then Beta 44 and returns 44.

Here I am blocked. Why b.h is not 44 and it is 4? Why does it take the value from Baap and not Beta (if it called the getH() from Beta, why not the value for h too?)

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Mon Apr 06, 2015 6:06 pm
by admin
ElizabethCM wrote:Why b.h is not 44 and it is 4? Why does it take the value from Baap and not Beta (if it called the getH() from Beta, why not the value for h too?)

Thanks
Because access to a variable depends on the class of the reference and not the class of the actual object (as is the case for access to a non-private instance method).
The fact that you are asking this question tells me that you are attempting mock exam questions without going through a book first. This concept is explained in any Java beginner book.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Sun Apr 26, 2015 1:49 pm
by ElizabethCM
Hi Paul,

You are right, I had this question before reading the inheritance chapter. It somehow appeared in another chapter of the exercises and not inheritance. Now, after reading the inheritance, I understand. Thanks

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Wed Jun 22, 2016 9:02 am
by wuqing1450
Here is my answer:

Baap b = new Beta();
b.h ==> it refers to h of Baap's instance based on the Type of reference, the value is 4
b.getH(); ==> Due to polymorphism, the subclass Beta's version will be invoked. Print "Beta 44" and return 44, so print "4 44"

Beta bb = (Beta) b;
bb.h ==> it refers to h of Beta's instance based on the Type of reference, the value is 44
bb.getH(); ==> Due to polymorphism, the subclass Beta's version will be invoked. Print "Beta 44" and return 44; so print "44 44"

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Sun Mar 19, 2017 11:33 am
by ioannemi42
admin wrote:Before the println is able to print the String, it has to form that String. So while it is building the String to print, it executes b.getH().
Hello,
I cannot understand this. Why is it calling the getH() before printing the variable??

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Sun Mar 19, 2017 11:40 am
by admin
ioannemi42 wrote:
admin wrote:Before the println is able to print the String, it has to form that String. So while it is building the String to print, it executes b.getH().
Hello,
I cannot understand this. Why is it calling the getH() before printing the variable??
How will it figure out the full string from the expression b.h + " " + b.getH() otherwise? It has three parts - b.h, " ", and b.getH() that it needs to concatenate. But it needs to get the value of g.getH() first before it can concatenate them, right? So it has to first call b.getH() before it can add all three.

-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Mon Mar 20, 2017 6:54 am
by ioannemi42
Hello Paul, I think I am almost there.
So we are saying that when System out has a method it first runs the method( getH) so the System.out.println which is inside the Method is printed.. but without returning the value.. and then b.h is printed and then the value that is returned from getH method?
Is that correct?

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Mon Mar 20, 2017 7:50 am
by admin
That is almost correct. Minor correction is that b.h is not printed separately. The three values - b.h, " ", and the value returned by getH are joined/concatenated and then the resulting string is printed at the end.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Tue Mar 21, 2017 6:22 am
by ioannemi42
ok thanks a lot !! :D

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Wed Aug 22, 2018 3:52 am
by md4422
admin wrote:
Mon Apr 06, 2015 6:06 pm
ElizabethCM wrote:Why b.h is not 44 and it is 4? Why does it take the value from Baap and not Beta (if it called the getH() from Beta, why not the value for h too?)

Thanks
Because access to a variable depends on the class of the reference and not the class of the actual object (as is the case for access to a non-private instance method).
The fact that you are asking this question tells me that you are attempting mock exam questions without going through a book first. This concept is explained in any Java beginner book.

HTH,
Paul.
Hi, I was surprised to learn this, and my book doesn't go into the why. Do you know of a resource that justifies this decision? It seems so strange to me.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Wed Aug 22, 2018 4:57 am
by admin
You can google for justifcation but the basic idea is that Polymorphism is about instance methods (and not about variables and static method) and that is why method access depends on the class of the actual object.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Sat Feb 16, 2019 11:49 am
by flex567
I think a part of the code runs like this

Because of Evaluation Order the following code

Code: Select all

System.out.println(b.h + " " + b.getH());
is evaluated to:

Code: Select all

(4 + " " + 44) //Before 44 is returned content of the getH() is run

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Sat Feb 16, 2019 7:41 pm
by admin
Correct.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Fri Apr 05, 2019 1:15 pm
by li_hanglin@bah.com
In this question, the answer says "b refers to an object of class Beta so b.getH() will always call the overridden (subclass's method)." In a similar question with QID: enthuware.ocajp.i.v8.2.996, why does it not call the overridden version instead but calls g.play()? Can you explain what differences I am missing? I am confused as to why the two answers are not consistent.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Fri Apr 05, 2019 10:52 pm
by admin
Because class Soccer does not override play() method of Game. Signature of play method in Soccer is play(String ). This is different from the play() method of Game.

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Wed Jul 24, 2019 4:04 am
by Krishna Kishore
Hello Paul

I have one doubt, You have mentioned that when there is a method inside the System.out.println(), then the method is first executed and then the other expression in the print statement. Is this always the case?

System.out.println(b.h + " " + b.getH());
So because of it
1. First b.getH() is executed
1.1. The print statement is executed first from the method
1.2 The int value is returned back
2. b.h is executed
3. Value from step-2 and step1.2 are concat to form 4 44

Is this correct ?

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Wed Jul 24, 2019 4:59 am
by admin
Krishna Kishore wrote:
Wed Jul 24, 2019 4:04 am
Hello Paul

I have one doubt, You have mentioned that when there is a method inside the System.out.println(), then the method is first executed and then the other expression in the print statement. Is this always the case?

System.out.println(b.h + " " + b.getH());
So because of it
1. First b.getH() is executed
1.1. The print statement is executed first from the method
1.2 The int value is returned back
2. b.h is executed
3. Value from step-2 and step1.2 are concat to form 4 44

Is this correct ?
You are misinterpreting what I said. Think about it. println(...) is just a method call with some argument. This argument could be a value, or an expression or a method call. To invoke println, the argument (whatever it is) has to be evaluated first, otherwise, what will you pass to the println method? So, yes, if the argument is a method call then that method has to be invoked first.

In this case, the argument is not a method call but an expression b.h + " " + b.getH(). The standard rules of evaluating an expression will apply.
To compute this expression, b.h+ " " has be computed and then the result has to be added with b.getH(). So, after computing b.h+" ", you need to get the value of b.getH().

Re: About Question enthuware.ocajp.i.v7.2.1370 :

Posted: Sun Jul 26, 2020 10:17 am
by Dreamweaver
I totally understand but I think I will not be able to see the trap on real exam..