• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing Method (inheritance and super method)  RSS feed

 
o moyse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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 !!!



 
Sudipta Laha
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
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!