I thought the out put will be 4 Beta 44 44 Beta 44
but out put is something like BETA 44 4 44 BETA 44 44 44
I am little confused how can "BETA" be printed first can you guys help me please
Thanks Charandeep
Padma Asrani
Ranch Hand
Joined: Mar 22, 2007
Posts: 111
posted
0
Hi Charandeep,
You example is funny. Anyways here is the explanation.
The method getH is returning integer value and when you are invoking
System.out.println(b.h + " "+b.getH());
b is pointing to Beta and it calls the getH method in Beta class where it prints System.out.println("BETA "+h); So the first output is BETA 44
Now after returning the value 44 it prints
4 44
Next you are calling
System.out.println(bb.h + " "+bb.getH());
this time you get BETA 44
then output of SOP will be
44 44
HTH Padma
Robert Horvath
Greenhorn
Joined: Jan 03, 2007
Posts: 15
posted
0
Hi Charandeep!
This is really a cool one :-)
In a statement like System.out.println(b.h + " " + b.getH()); all singe expressions (b.h, b.getH()) are executed first before the whole statement is executed.
This mean, that 1. b.getH() is executed, thus BETA 44 is written to the output! 2. b.getH() returns 44, so the statement looks afterwards like System.out.println(b.h + " " + 44);
3. next b.h is executed, so the statement looks afterwards like System.out.println(4 + " " + 44);
4. finaly, the statement is executed and the output is 4 44
So the whole problem is actually more about execution order within a statement than about overriding.
When the line System.out.println(b.h + " " + b.getH()); is executed, Java needs a singleString as an argument for println. In the process of evaluating the String expression, it calls b.getH(), and this causes "BETA 44" to be output before "44" is returned by that method. After b.getH() returns, the String "4 44" (a concatenation of "4" + " " + "44") can be passed to the println method.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Michael Ku
Ranch Hand
Joined: Apr 20, 2002
Posts: 510
posted
0
It is a combination of a few things - direct access to variables, executing overridden methods in objects and the immutability of Strings
Strings are immutable - so every time you use the '+' String operator you create a new String. In preparation for this the "=" operator evaluates each part of the expression. It evaluates b.h + " "+b.getH() in pieces and will coerce each piece into a String before creating the final String b.h evaluates to 4 because the reference variable of type Baap has h = 4 " " evaluates to " " b.getH() evaluates to the get method of the underlying method which prints "Beta" + h (this in turn evaluates to BETA 44 before returning 44 Having already printed BETA 44 it now creates a new String from the pieces (4," ", and 44) which yields 4 44
So the result of the first 2 lines is: BETA 44 4 44
You can use similar logic to figure out the rest. Keep in mind that when you overide methods, regardless of the reference variable, the method of the underlying object is executed. Direct access to properties will attempt to use the reference variable type first (if both class and superclass share the same variable name. This is why you should always go after a property with a getter methos
madhu v pe
Ranch Hand
Joined: Apr 21, 2007
Posts: 100
posted
0
Michael,
Can you elaborate this statement?
Originally posted by Michael Ku:
Direct access to properties will attempt to use the reference variable type first (if both class and superclass share the same variable name. This is why you should always go after a property with a getter methos
Thanks
Charandeep Singh
Ranch Hand
Joined: May 06, 2007
Posts: 57
posted
0
Hi All
Thanks a lot for all your explanations I finally got this one but I have a query in this currently in the above code we just have one method getH() what if we had a similar kinda of method getJ() and that is also part of output I tried to run it prints from left to right i.e. which method is encountered first by SOP that is first to executed is it always that or some exceptions ???
Thanks Charan
Michael Ku
Ranch Hand
Joined: Apr 20, 2002
Posts: 510
posted
0
madhu, just look at what happened when you accessed b (Baat b = new Beta()). The reference variable is of type Baat but the underlying object is of type Beta. Each has a variable "h" and a method getH(). When you acces 'h' directly you get the value from BAAT (the reference variable type). When you acces 'h' with a getter method, you get the value from Beta ( the underlying object)
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.