• 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

"Appropriate" ToString() Override--I am so confused!

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going through the OCP Java SE7 Programmer Certification Guide V11 (work in progress) by Mapa Gupta. She says you shouldn't use names of classes in the override, but then seemingly contradicts that.

First: "You override the toString inappropriately, if it returns any text that is specific to a particular class, for example, name of class or value of a static variable:..."

Then she goes on to show examples of inappropriate overriding, but only with static variables, not names of classes. OK.

Then there is a Twist in the Tale Exercise asking which overrides are appropriate and inappropriate:


So I marked this as inappropriate because it returns the name of the class. But, she seemingly contradicts herself in the answer key, when she says:
"The method toString in classes Book3 and Book4 returns the value of title and its class name-which also exhibit appropriate definition of this method."
What???
So then I google all over the place, and nowhere does it say returning the names of classes is inappropriate, and in fact one source says it's good practice.

Somebody please help.
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I can't get the code formatting to work! I chose Java and Code, and placed the tags before the code snippet, but it refused to format. It took out my blank spaces. And if I could figure out how to fix it, which must be simple, it won't let me edit my post any more.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrea,
For the code tag the [/code] tag needs to after the code. That way the code tags wrap the code. I fixed this for you.

I think Mala means that this is bad:
[code=java] return "Book:" + title; [/code]

It hard codes the name of the class. If the class name were to change, the toString() method would be wrong. Making this brittle. Calling getName() would get whatever the current name of the class is and therefore is fine.
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhhh...well of course hard coding the current name of the class or anything that could change, is poor practice. And I feel so stupid about code tag thing.

Thank you, and thank you!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic