About Question enthuware.ocajp.i.v8.2.1482 :

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

Moderator: admin

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

About Question enthuware.ocajp.i.v8.2.1482 :

Post by ElizabethCM »

test.png
test.png (16.13 KiB) Viewed 3780 times
Hello,

I need some help here with the observations done at this exercise:
It says:
Always remember: Instance methods are overridden and variables are hidden.
Which method is invoked depends on the class of the actual object, while which field is accessed depends on the class of the variable.

so when doing
 Baap b = new Beta();         
System.out.println(b.h+" "+b.getH());
The class of the object is Beta. Which is the class of the variable? And which is exactly the variable in this case?

Thanks a lot

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

b is the variable, b's class is Baap. So b.h will refer to Baap's h.
But the class of the object referred to by b is Beta, therefore, b.getH() will invoke Beta's getH.
If you like our products and services, please help us by posting your review here.

wiseco68
Posts: 2
Joined: Thu Sep 10, 2015 10:26 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by wiseco68 »

I understand which methods and variables are supposed to print, but why in that order?
The print line is:
System.out.println(b.h+" "+b.getH());
But b.h is the third out of four things printed.

Thank you very much, and all of your responses that I have read have been very helpful.

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

Before the println method can print the string generated by the expression b.h+" "+b.getH(), the expression has to be evaluated to generate the string. While computing the value of this string, it calls b.getH(). The call itself contains a println, which prints the value that you see.
So basically, b.h gets printed later because of the println calls made while computing the expression.
If you like our products and services, please help us by posting your review here.

wiseco68
Posts: 2
Joined: Thu Sep 10, 2015 10:26 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by wiseco68 »

Great. Thanks!
I couldn't find an order of precedence on this in the Java Specifications. Do you have a link that describes how to evaluate this? Closest I could find was 15.12.4 and that was clear as mud.
My answer had the right values but in the wrong order.

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

It is not about precedence of operators. Let's say you want to compute 10+y. To do that you have to first get the value of y, right? In this case, y is a method call b.getH(), which has to be invoked first before the call println can be invoked.
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by heleneshaikh »

I do not understand the order of the output given in the answer.
I had this order:
4 Beta 44 44 for System.out.println(b.h + " "+b.getH());
44 Beta 44 44 for System.out.println(bb.h + " "+bb.getH());

Thanks in advance for clarifying this

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

Please go through the responses above. They explain exactly what you are asking.
If you like our products and services, please help us by posting your review here.

KKreator
Posts: 2
Joined: Tue May 17, 2016 2:33 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by KKreator »

heleneshaikh wrote:I do not understand the order of the output given in the answer.
I had this order:
4 Beta 44 44 for System.out.println(b.h + " "+b.getH());
44 Beta 44 44 for System.out.println(bb.h + " "+bb.getH());

Thanks in advance for clarifying this
Try to think how the methods are executed on the stack. And remember that the last method on the stack (in this case b.getH()), is executed first - LIFO (Last In First Out), and after that the returning value is sent to the first println( System.out.println(b.h + " "+b.getH()); ), evaluated and executed.

asenevtimov
Posts: 2
Joined: Mon Oct 16, 2017 5:48 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by asenevtimov »

Hi all, please someone explain why bb.getH() method calls the the method getH() from the Baap class. bb refer to an object of class Beata, so it should call Beata's getH(). Which overridden method is called depends on the object type isn't it?

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

asenevtimov wrote:Hi all, please someone explain why bb.getH() method calls the the method getH() from the Baap class. bb refer to an object of class Beata, so it should call Beata's getH(). Which overridden method is called depends on the object type isn't it?
Yes, bb.getH() does indeed call Beta's getH. Why do you think it calls Baap's getH?
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by Javier »

Hi!!

I would like to know why the statement:

System.out.println(b.h+" "+b.getH());

is not printing:

Beta 44
4 Beta 44 //Because b.getH() is saying to print "Beta 44".

Why the first time calling b.getH() is printing "Beta 44" and the second line is just returning the h value?

Thank you very much Enthuware!

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

The statement System.out.println(b.h+" "+b.getH()); will print whatever string is computed by combining b.h , " " , and the value returned by calling b.getH(). So to compute that value, it has to first call b.getH(). In the call to b.getH(), there is another System.out.println, which prints "Beta "+h, which prints "Beta 44" and it returns the value of h i.e. 44.

Now, the value of the first println statement can be computed i.e. 4+" "+44, so "4 44" is printed.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by Javier »

Great explanation!!

Thank you very much guys!!

Post Reply

Who is online

Users browsing this forum: marpiva and 35 guests