About Question enthuware.ocajp.i.v8.2.1482 :
Moderator: admin
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1482 :
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
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.
-
- Posts: 2
- Joined: Thu Sep 10, 2015 10:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
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.
-
- Posts: 2
- Joined: Thu Sep 10, 2015 10:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
-
- Posts: 2
- Joined: Tue May 17, 2016 2:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.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
-
- Posts: 2
- Joined: Mon Oct 16, 2017 5:48 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
Yes, bb.getH() does indeed call Beta's getH. Why do you think it calls Baap's getH?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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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!
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!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
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.
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.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1482 :
Great explanation!!
Thank you very much guys!!
Thank you very much guys!!
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 122 guests