• 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

print wrong output in extended class

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?!


 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Discussed here: http://enthuware.com/forum/viewtopic.php?f=2&t=2075&p=14993&hilit=baap#p14993

HTH,
Paul.
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! I see, thank you Dave
 
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

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