Reputation: 55
I have an abstract class, Mower, that overrides the toString() method. The Lawn Tractor class extends Mower. It overrides the toString() method and calls super.toString() as the first line in the method. I then have a Commercial class that extends LawnTractor and overrides the toString(), calling super.toString() in its first line as well. When instantiate a Commercial object (declared as a Mower object) and call its toString() I expect it to print both properties from Mower and Lawn Tractor and then it's own properties. Instead, it prints only the properties from Mower and then it's own properties. How do I call or override the toString() method in Commercial so that it calls its parent (LawnTractor) toString() and not just the super class (Mower).
public abstract class Mower {
private String manufacturer;
private int year;
private String serialNumber;
public String tostring(){
String temp = "Manufacturer: " + getManufacturer() + "\nYear: " + getYear() + "\nSerial Number:" + getSerialNumber() + "\n";
return temp;
}
}
public class LawnTractor extends Mower{
private Engine engine = new Engine();
private String model;
private double deckWidth;
public LawnTractor(){
super();
}
public String toString(){
String temp = super.tostring() + "Engine:\n" + getEngine().toString() + "\nModel: " + getModel() + "\nDeck Width: " + getDeckWidth() + "\n";
return temp;
}
public class CommercialMower extends LawnTractor {
private double operatingHours;
private boolean zeroTurnRadius;
public CommercialMower(){
super();
}
public String toString(){
String temp = super.toString() + "Operating Hours: " + getOperatingHours() + "\nZero Turn Radius: " + isZeroTurnRadius() + "\n";
return temp;
}
Upvotes: 2
Views: 134
Reputation: 55
I realized my mistake. My Mower class has tostring() method but does not override the toString() method (difference in syntax).
Upvotes: 0
Reputation: 1628
So it sounds like you are trying to do is create a hierarchy of toStrings to print when you have a graph of objects. So all you need to do is append the super's toStrings as they are called up the chain. (If I'm understanding you correctly)
Here's the Mower class
public class Mower {
@Override
public String toString() {
return "Mower toString\n";
}
}
LawnTractor
public class LawnTractor extends Mower {
@Override
public String toString() {
String superString = super.toString();
return superString + "LawnTractor toString\n";
}
}
Commercial
public class Commercial extends LawnTractor {
@Override
public String toString() {
String superString = super.toString();
return superString + "Commercial toString\n";
}
}
Main class to run it. See the output below. Is this where you are going with this.
public class Main {
public static void main(String[] args) {
Mower mow = new Commercial();
System.out.println(mow.toString());
}
}
Here's your output. I believe this is what you're looking for. If not please clarify
Mower toString
LawnTractor toString
Commercial toString
The order of how you want the objects to show up in the return string can be reordered depending on how you append them together. Mower can be first or last.
Upvotes: 2