Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRey Adronico Baguio
13,672 PointsDidn't understand how inheriting the toString() method from Objects changed the Treet return value.
In Object Inheritance, Craig was able to re-format the return value from instantiating a treet. Before using the inherited method toString() (from Objects), the return value from instantiation was something like "com.treehouse.Treet@7852e922" which I understand as the directory of the specific Treet that was instantiated. After overriding the toString() method, that changed to a textual representation of the Treet itself (with author, Description, etc...)
2 Answers
Allan Clark
10,810 PointsLook through this post for more info about the toString() method. https://teamtreehouse.com/forum/where-is-the-tostring-call
Hit us with any other questions you have.
Jacob Bergdahl
29,119 PointsOverriding toString() means that every time you try to call your object as a string, whatever return value you have in toString() is returned. What exactly is is that you don't understand -- you seem to have understood it pretty well just based on your question.
You simply define how an object should be presented when converted to a string.
Marius Wallin
11,464 PointsHi. Does that mean that all Strings from that same class you call will have the same value returned? If you present the output like a string (eks. ("This would be know as a string") // will be overridden with your toString value.
Allan Clark
10,810 PointsEvery instance of the class will have the same behavior defined in the toString() method. If you were to define that behavior to return just a default string, every instance of the class will return that default string.
lets say we defined the Treet class to return "This is a Treet" in the toString() method, the class would behave like this
Treet t1 = new Treet();
Treet t2 = new Treet();
System.out.println(t1);
System.out.println(t2);
the output will look like this:
This is a Treet This is a Treet
Marius Wallin
11,464 PointsThanks Allan.
Rey Adronico Baguio
13,672 PointsRey Adronico Baguio
13,672 PointsNice! That clears it up. That helped a lot. Thanks!