Page 1 of 1

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

Posted: Sat May 23, 2015 2:56 pm
by ElizabethCM
test.png
test.png (16.13 KiB) Viewed 4063 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

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

Posted: Sat May 23, 2015 8:21 pm
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.

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

Posted: Wed Sep 30, 2015 12:06 pm
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.

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

Posted: Thu Oct 01, 2015 12:18 am
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.

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

Posted: Thu Oct 01, 2015 12:16 pm
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.

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

Posted: Thu Oct 01, 2015 7:43 pm
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.

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

Posted: Mon Oct 05, 2015 5:50 am
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

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

Posted: Mon Oct 05, 2015 7:51 am
by admin
Please go through the responses above. They explain exactly what you are asking.

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

Posted: Tue May 17, 2016 3:01 am
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.

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

Posted: Mon Oct 16, 2017 5:59 am
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?

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

Posted: Mon Oct 16, 2017 10:38 pm
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?

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

Posted: Sun Oct 22, 2017 2:12 pm
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!

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

Posted: Sun Oct 22, 2017 10:10 pm
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.

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

Posted: Mon Oct 23, 2017 8:14 am
by Javier
Great explanation!!

Thank you very much guys!!