This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print wrong output in extended class

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ANother one of the mock up exam question has this code:

Now, this apparently prints
Beta 44
4 44
Beta 44
44 44
I couldn't figure out how it printed that, so I run the class here in the console, and I do get indeed that output (I thought it might have been a mistake or something). Problem is, still I don't understand how it gets that output. I'm not talking about the concept of polymorphism as such, I'm just referrring to the actual output. The first print statement executed is   which prints an integer and not a string so how come the output starts with Beta and not with an integer?!


 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the first line that gets printed.

What happens in that b.getH() call that occurs before the println() method is actually entered on line 16?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In print statement execution starts from left so
Step 1: b.h is 4, available for String concatination means 4 + " " now we have "4 " then goes for next concatination "4 "+ b.getH()
Step 2: b.getH() is executed so in getH() method It prints Beta 44 and returns  44 so now we have "4 "+ 44 which becomes "4 44" then print statement is ready to print which prints 4 44
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I was wrong, I tried this same result means as Dave said b.getH() call that occurs first in println() method
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason Attin,
First You should observe two things
1.getH() is overriden.
2. variable h is hidden( it has same name and access modifier in both super and subclass).



Here, object is created for the sub class and reference type is super class.



In the above line there is method call. First that will be executed in the sub class overridden one . There is  print statement which prints Beta 44 and returns 44 to the main method.

b is Baap type. so it gives 4. the output is 4 44. 

Here b is casted to Beta type. Here also thing happens


In the above line there is method call. First that will be executed in the sub class overridden one . There is  print statement which prints Beta 44 and returns 44 to the main method.



here bb is Beta type which gives 44. so the output is 44 44.

So the overall output is

Beta 44
4 44
Beta 44
44 44

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Paul 
In my first post I explained what I thought, how It might get executed because of String concatination, was that correct then? when I used prinf(...) that baffled me because printf() doesn't seem to use String concatination.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not the String concatenation as such.

In order to supply the parameter to println() there needs to be a single String.
In order to get that single String you need to concatenate the values together.
In order for that to happen you need the result returned by the call to getH().

All of that happens before the println() method is entered.  That is all about getting the parameter for the method call.

Similarly with your printf().
In order to get the last parameter for the printf the call to getH has to occur, so it can return the String.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! I see, thank you Dave
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Problem is, still I don't understand how it gets that output. I'm not talking about the concept of polymorphism as such, I'm just referrring to the actual output.

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic