in the abstract class:

public String toString() { return "distance = " + String.format("%.3f", way) + " moving speed = " + String.format("%.3f", SPEED) } 

in the descendant from the abstract:

 public String toString() { return "RideOnLine: " + super.toString(); } 

in the descendant from the descendant:

 public String toString() { return "RideOnCurve: " + super.toString(); } 

as a result, the output: RideOnCurve: RideOnLine: distance = 60,000 moving speed = 60,000. Tell me how to correctly do so that there is no line (in italics) from the descendant of the abstract?

    2 answers 2

    The very essence of the redefinition of methods is that all children of the parent class who redefined a method should see the "new" version of the method.

    You want to be able to get the "clean" result of the toString() method of an abstract class for all descendants in the hierarchy. To do this, make this method separately (if necessary - into the final one) and use it as a descendant. Example of implementation:

     abstract class AbstractClass { private float way = 60000; private float SPEED = 60000; @Override public String toString() { return formatData(); } protected final String formatData() { return String.format("distance = %.3f moving speed = %.3f", way, SPEED); } } class Parent extends AbstractClass { @Override public String toString() { return "RideOnLine: " + formatData(); } } class Child extends Parent { @Override public String toString() { return "RideOnCurve: " + formatData(); } } 
       class Abstract { protected String getInfo() { return "distance = " + String.format("%.3f", way) + " moving speed = " + String.format("%.3f", SPEED); } @Override public String toString() { return getInfo(); } } class Child1 extends Abstract { @Override public String toString() { return "RideOnLine: " + getInfo(); } } class Child2 extends Child1 { @Override public String toString() { return "RideOnCurve: " + super.toString(); } } 

      Or

       class Abstract { @Override public String toString() { return "distance = " + String.format("%.3f", way) + " moving speed = " + String.format("%.3f", SPEED); } } class Child1 extends Abstract { protected String getPrefix() { return "RideOnLine: "; } @Override public String toString() { return getPrefix() + super.toString(); } } class Child2 extends Child1 { @Override protected String getPrefix() { return "RideOnCurve: "; } }