wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Am I doing toString override properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Am I doing toString override properly?" Watch "Am I doing toString override properly?" New topic
Author

Am I doing toString override properly?

James Blarney
Greenhorn

Joined: Oct 02, 2009
Posts: 10
Could someone suggest how I would impliment public String toString in the code below. From what I understand, toString tries to describe an object, but I dont know how to scupt toString output to get what I want. I am supposed to do this:
Override

public String toString() { ... }

in each class. The override should return a string that consists of

(a) the toString() value returned from super.toString()
(b) concatenated with the fields in that class.

For instance, Emp's toString might

return super.toString() + "\n" +
"First name: " + first_name + "\n" +
"Last name: " + last_name;

But I cannot find any more information on how to do it. Could someone point me in the right direction. I am referring to line 10 to 16. Im not asking that anyone do this for me, but am I OVER RIDING by simply typing public String toString() { or am I missing something.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
Well, what happens when you test it?

If you get the output you expect, then you did it properly.

If you don't get the output you expect then your did it incorrectly.
James Blarney
Greenhorn

Joined: Oct 02, 2009
Posts: 10
I am not getting the intended output.
The current output is:


I need to override this output with


The problem is, I dont really know how to implement this override.

Any suggestions?
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Matthew Brady wrote:The problem is, I dont really know how to implement this override.


Well, you have overridden toString correctly in Emp (if it produces the desired result is another story).

The problem is that you call the hi method instead of the toString method.
James Blarney
Greenhorn

Joined: Oct 02, 2009
Posts: 10
So I am over riding it. but the program still prints Emp Greetings. Should I be trying to over ride the Hi method?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
uj has told you: you are calling "hi" instead of toString somewhere.

I would say you ought to use String.format and the %n tag instead of \n but that is of secondary importance to your present problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Actually you shouldn't override "hi" at all. Implement it once in the Emp class like this

. . . return getClass().getSimpleName() + " greetings";

Then you can probably get rid of all your overridings.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Matthew Brady wrote:So I am over riding it. but the program still prints Emp Greetings. Should I be trying to over ride the Hi method?


You do this now,

for (Emp emp : emps) emp.hi();

As you see you're calling the hi method. To invoke the toString method you instead do,

for (Emp emp : emps) System.out.println(emp):

The println method will call toString behind the scenes and print the String it returns.
 
Don't get me started about those stupid light bulbs.
 
subject: Am I doing toString override properly?