Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt about Over ridding

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi while doing a test series

I had this question


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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Cheers,
Robert
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the line System.out.println(b.h + " " + b.getH()); is executed, Java needs a single String 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.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
He baked a muffin that stole my car! And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic