• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Printing Method (inheritance and super method)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I have been studying java for over a week,it was going well until now

I have a question about inheritance and super method

I have 3 classes Person, Student , Employee.


I was expecting something like

"joe",25,"new york","2212-55-1212
"bob",40,"boston","617-55-1212",20000.99


So now i am confused

Can you help me !!!



 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..

you are printing the objects not the values of your member variables. so you are getting such results.
try to get the object from your array and try using the getter methods to get your desired output.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

You where well on your way but you need to override the toString in person. Because the Object's implementation of it just return the classname + @ + memorylocation in hex.
 
o moyse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your quick reply

I used System.out.println("Name " +arr[i].getName()+" Adress: "+arr[i].getAdress());

I see what you mean now, it was a refence to the object.

 
o moyse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just out of curiosity is there a better way to print the values of my objects or a better data structure to store an get the values of my objects ???

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The use of the toString() method is a common way to print the values of an Object. It's the reason behind the existence of the method. It is sufficient for simple printing/debugging but when it gets complexer you should separate data and presentation. In your case you could replace the the for loop and use the enhanced for loop instead. Also the System.out.println() method automatically uses the toString() method is the parameter is an Object reference (i.e. not a primitive) and the value is not null. So the toString() is unnecessary.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic