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 trial

Java Java Data Structures Getting There Object Inheritance

Rey Adronico Baguio
Rey Adronico Baguio
13,672 Points

Didn'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
Allan Clark
10,810 Points

Look 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.

Rey Adronico Baguio
Rey Adronico Baguio
13,672 Points

Nice! That clears it up. That helped a lot. Thanks!

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

Overriding 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
Marius Wallin
11,464 Points

Hi. 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
Allan Clark
10,810 Points

Every 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