Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Question about what method is executed in inheritance

 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code




What does it print this code?

I think the output is

4 Beta 44
44 Beta 44

Could anyone explain me what is the output of this code?
 
Creator of Enthuware JWS+ V6
Posts: 3339
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Quote-Your-Sources
 
Carlota Vina
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlota Vina wrote:I have the following code

This question is in Enthuware Objective-wise tests working with Inheritance question 8




What does it print this code?

I think the output is

4 Beta 44
44 Beta 44

Could anyone explain me what is the output of this code?

 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3339
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are really close. When I run it, it outputs:

Beta 44
4 44
Beta 44
44 44


What part did you not get?
 
Carlota Vina
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first print is b.h b.getH()

It could be 4 Beta 4
 
Carlota Vina
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlota Vina wrote:The first print is b.h b.getH()

It should be 4 Beta 4

 
Carlota Vina
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlota Vina wrote:

Carlota Vina wrote:The first print is b.h b.getH()

It should be 4 Beta 44

 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3339
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before System.out.println(b.h+" "+b.getH()); is completely finished it has to execute b.getH(), and in the execution of that method there is a System.out.println("Beta " + h);.

Does that make sense to you?
 
Carlota Vina
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the order it b.getH() and after the System.out.println?
 
Marshal
Posts: 69373
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlota Vina wrote:Then the order it b.getH() and after the System.out.println?

That doesn't look clear, but I think the answer is no.
You go from left to right. The order is therefore,
  • 1: Find system class
  • 2: Find its out field
  • 3: Find its println() method, but that cannot complete until it “knows” what its argument is.
  • 4: Evaluate the argument for println(), inside the (), starting with "Beta "
  • 5: Then execute the + operation
  • 6: The + operator finds it is necessary to run the getH() method first. I shall abbreviate getH() to one line and forget that it includes a print instruction. When getH() returns, the + operator can complete its task.
  • 7: Now that the argument for println() is complete, the println() method can complete its execution
  • At least I think that is right.
     
    Paper jam tastes about as you would expect. Try some on this tiny ad:
    Devious Experiments for a Truly Passive Greenhouse!
    https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
      Bookmark Topic Watch Topic
    • New Topic