• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding overriding and shadowing

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

The following question comes from one of Enthuware's mock exams. If I'm not allowed to post this: my apologies, and I'll remove the post.

What will the following code print when run?

The output is:


Beta 44
4 44
Beta 44
44 44


I don't understand how this is possible. As far as I can tell, the very first variable that should be printed by this code is b.h, which is 4. How can Beta be printed first?

Thanks again for your help

Regards,
Shane
 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shane.

The expression needs to be evaluated first.

b.h evaluates to 4, because b is of type Baap, and it defines the value 4 for the instance variable h. Then, the b.getH() expression will be evaluated, but since it is a method that returns a value, the method is executed first, and inside  the getH() method, there is a System.out.println() printing the value of h, and then it will return this h value to complete the evaluation of the expression 1. So, first the method getH() is executed, and it will return a value. Then, after the evaluation of expression 1 is completed, it will print the resulting values.
Pay attention to the dynamic binding here. The variable b is of type Baap, but its object is of type Beta, so at runtime the getH() from Beta class will be used, and it will print Beta and 44 (the value of h inside Beta).
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

João Victor Gomes wrote:Hello Shane.

The expression needs to be evaluated first.

b.h evaluates to 4, because b is of type Baap, and it defines the value 4 for the instance variable h. Then, the b.getH() expression will be evaluated, but since it is a method that returns a value, the method is executed first, and inside  the getH() method, there is a System.out.println() printing the value of h, and then it will return this h value to complete the evaluation of the expression 1. So, first the method getH() is executed, and it will return a value. Then, after the evaluation of expression 1 is completed, it will print the resulting values.
Pay attention to the dynamic binding here. The variable b is of type Baap, but its object is of type Beta, so at runtime the getH() from Beta class will be used, and it will print Beta and 44 (the value of h inside Beta).


Hi João,

Thanks for your reply. How does this explain that Beta is printed before everything else? Even before b.h?
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shane Jensen wrote:How does this explain that Beta is printed before everything else? Even before b.h?



As I said, the expression needs to be fully evaluated, so before the following code prints anything, it must evaluate b.h and b.getH()

First, it evaluates b.h to 4, but before printing 4 44, it must define the value of b.getH(). And how it is done? By calling the method getH() that returns the value to be used in the expression. But if you are calling a method, the method must be executed completely before it returns the value, and since this method is printing - System.out.println("Beta " + h); - before returning the value to be used in the first expression, that's why you get Beta 44 before 4 44.

At line 1, when b.getH() starts to be evaluated, this line stops for a moment, and the method getH() is executed, printing Beta 44 and returning the value of h, finishing its execution, and then the control returns to the expression represented in line 1, so it can complete its execution.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

João Victor Gomes wrote:

Shane Jensen wrote:How does this explain that Beta is printed before everything else? Even before b.h?



As I said, the expression needs to be fully evaluated, so before the following code prints anything, it must evaluate b.h and b.getH()

First, it evaluates b.h to 4, but before printing 4 44, it must define the value of b.getH(). And how it is done? By calling the method getH() that returns the value to be used in the expression. But if you are calling a method, the method must be executed completely before it returns the value, and since this method is printing - System.out.println("Beta " + h); - before returning the value to be used in the first expression, that's why you get Beta 44 before 4 44.

At line 1, when b.getH() starts to be evaluated, this line stops for a moment, and the method getH() is executed, printing Beta 44 and returning the value of h, finishing its execution, and then the control returns to the expression represented in line 1, so it can complete its execution.


Right. So if I understand correctly: all parameters within a single System.out.println() statement need to be evaluated before anything can be printed? That's new to me, but it does make sense. Thank you for your detailed explanation!
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another example

It will print

Returning a value...
The value is: 5

Because before printing anything, the method getValue() is called, and it will print Returning a value..., then it will return 5, and the println inside the main method will print The value is: 5
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shane Jensen wrote:
Right. So if I understand correctly: all parameters within a single System.out.println() statement need to be evaluated before anything can be printed? That's new to me, but it does make sense. Thank you for your detailed explanation!



Remember that String concatenation follows a set of rules.

The expression is evaluated left to right:
b.h + "  ": In this case, one of the operands is a String, so the + operator means concatenation, and it will result in "4  " (without the quotes)
Then, "4  " + b.getH() will be another concatenation, but first the method getH() needs to be called in order to return a value to be concatenated, and this method prints something before returning the value.
Concluding, before the concatenation concludes to print the result of println, the method needs to be called.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The println() method requires a String parameter.
Consequently that String has to be calculated first.  You are not passing the expression 'b.h + " " + b.getH()' into println(), you are passing the result of that expression.
So, before println can actually be executed, that expression (and hence b.getH()) must be executed first.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shane Jensen wrote:The following question comes from one of Enthuware's mock exams. If I'm not allowed to post this: my apologies, and I'll remove the post.


You have quoted your sources, so it's fine

Shane Jensen wrote:I don't understand how this is possible. As far as I can tell, the very first variable that should be printed by this code is b.h, which is 4. How can Beta be printed first?


This code snippet (and its output) is already explained in great detail in this topic. So I'm pretty sure when you have carefully read this excellent topic you'll have a perfect understanding why this output is printed. If you still have doubts and/or questions, just let us know by replying to this topic.

Hope it helps!
Kind regards,
Roel
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for the link. That definitely helps!

Shane
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic