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
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 71 guests