This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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
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
Joined: May 06, 2007
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 ???
Joined: Apr 20, 2002
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)