About Question enthuware.ocajp.i.v7.2.1370 :
Moderator: admin
-
- Posts: 3
- Joined: Sun Mar 19, 2017 10:44 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
ok thanks a lot !!
-
- Posts: 3
- Joined: Tue Aug 21, 2018 4:44 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
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 wrote: ↑Mon Apr 06, 2015 6:06 pmBecause 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).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
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
I think a part of the code runs like this
Because of Evaluation Order the following code
is evaluated to:
Because of Evaluation Order the following code
Code: Select all
System.out.println(b.h + " " + b.getH());
Code: Select all
(4 + " " + 44) //Before 44 is returned content of the getH() is run
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
Correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Mon Jan 28, 2019 4:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
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.
-
- Posts: 1
- Joined: Wed Jul 24, 2019 3:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
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 ?
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 ?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
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.Krishna Kishore wrote: ↑Wed Jul 24, 2019 4:04 amHello 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 ?
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.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1370 :
I totally understand but I think I will not be able to see the trap on real exam..
Who is online
Users browsing this forum: No registered users and 71 guests