Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Am I doing toString override properly?

 
James Blarney
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic