This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Question about what method is executed in inheritance  RSS feed

 
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: 3220
283
Android Chrome Eclipse IDE
  • 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: 3220
283
Android Chrome Eclipse IDE
  • 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: 3220
283
Android Chrome Eclipse IDE
  • 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: 62259
193
  • 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.
     
    I promise I will be the best, most loyal friend ever! All for this tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!