Refer to the class of the this
instance, which will be the actual class (possibly a subclass):
public abstract class Fish {
// rest of class omitted
@Override
public String toString() {
return "A " + size + "cm, " + weight + "kg " + getClass().getSimpleName();
}
}
The call getClass().getSimpleName()
will return the String "AnotherFish"
etc.
No need to define anything in a subclass to make this work.