Win a copy of Python Continuous Integration and Delivery this week in the Python 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

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: 3246
287
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: 3246
287
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: 3246
287
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: 63383
205
  • 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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!