ARTICLE AD BOX
I lately learned about the toString method and its uses.
From what I've been taught, the toString method is used as a concise representation of an object as a string of characters, usually used as a debugging tool.
My question is how should I format my toString methods so they would provide a concise reflection of the object's values at the moment?
Let's e.g. say that I have the next classes:
abstract public class Vehicle { public FuelTank fuelTank; public String model; @Override public String toString() { return ??? } } public class Truck extends Vehicle { public Cargo cargo; public Truck() { fuelTank.addFuel(5); model = "Strong Truck"; cargo.maxWeightInKG(5); } @Override public String toString() { return super.toString() + ??? } } public class Garage() { Truck truck; @Override public String toString() { return ??? } }In the specified scenario, we have inheritance and composition of classes, which made it quite difficult for me to find toString methods' return values that would return comprehensible reflections of the objects they describe. What would be the best/conventional way to implement the toString methods' return values for such a use (concise reflection of the object's values at a moment)?
Thanks!
