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 trialBen Summers
1,306 PointsWhere is the toString() call?
I follow the class specific method we have created here, and I get the concept of inheritance, override and polymorphism.
But what eludes me is how our changes to the superclass method toString() effect what is printed out in the main method.
I guess that when we use the %s notation in the printf method, the object we pass (i.e. treet) has the toString method called on it, but I'm probably wrong.
Could someone explain, because this seems to be glossed over in the lesson.
Many thanks.
6 Answers
Allan Clark
10,810 PointsThe default toString() method inherited from the Object class will print out a bunch of computer language which is a physical memory reference (and not very useful to a human). Overriding the toString() method gives you the power to control how your Class behaves when printf() or println() is called on it.
For example say we have a Car class:
public class Car{
private String make;
private String model;
private String year;
//plus other code skipped for space
}
If we don't override the toString() method, printing an instance of the Car class(i.e. printf(car); ) will print out the memory reference to the console. Letters and numbers and the name of the class.
// toString() we can customize what is printed out.
// So we could make the car print out the make then model and year
@Override
public String toString() {
return "Make: " + make + "\nModel: " + model + "\nYear: " + year;
}
Car car = new Car("Ford", "Mustang", "2015");
System.out.printf(car);
// this will now print:
// Make: Ford
// Model: Mustang
// Year: 2015
Hope this helps!
Ben Summers
1,306 PointsThanks very much, but its not quite answering the question I asked, but perhaps my question is not clear.
I think the answer I'm looking for is wrapped up in your clause 'how your Class behaves when printf() or println() is called on it'.
If I understand you, the toString() method does not have to be called by our code explicitly; it is called when the object is passed to printf() or println().
Right?
Allan Clark
10,810 PointsExactly, it does not need to be explicitly coded.
printf(car);
is the same as
printf(car.toString);
Taha Abbasi
12,865 PointsThank you! I had the same question. Clarifies a lot here! :)
Greg Andrews
7,554 PointsThanks! I just had the same question and this answered it
Rogelio Valdez
6,244 PointsThat was exactly what I was wondering!! Thank you!!
Dennis Saadeddin
1,303 PointsThank you! I had the same question, beautiful reply! :D keep up the good work!