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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post 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...

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post 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

wuqing1450
Posts: 8
Joined: Thu Mar 24, 2016 12:02 pm
Contact:

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

Post 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"

ioannemi42
Posts: 3
Joined: Sun Mar 19, 2017 10:44 am
Contact:

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

Post 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??

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ioannemi42
Posts: 3
Joined: Sun Mar 19, 2017 10:44 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ioannemi42
Posts: 3
Joined: Sun Mar 19, 2017 10:44 am
Contact:

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

Post by ioannemi42 »

ok thanks a lot !! :D

md4422
Posts: 3
Joined: Tue Aug 21, 2018 4:44 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

li_hanglin@bah.com
Posts: 6
Joined: Mon Jan 28, 2019 4:18 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Krishna Kishore
Posts: 1
Joined: Wed Jul 24, 2019 3:57 am
Contact:

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

Post 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 ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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().
If you like our products and services, please help us by posting your review here.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

I totally understand but I think I will not be able to see the trap on real exam..

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests