I\'m developing an SPI and would like to define a Reportable interface such that any implementations must override toString() to something that is
What I understand is that you want to create a set of classes which neatly give their string representations. So that when something like System.out.println(yourobject) is called it shows meaningful data.
You cannot force your subclasses to override toString. But you can do something like this.
abstract class MyBase
{
abstract String getNiceString();
@Override
public String toString()
{
return getNiceString();
}
}
No, you can't do this. I'd suggest you choose a different method name, e.g.
public interface Reportable
{
String createReport();
}
That will force implementations to write an appropriate method. toString() is already somewhat vague in its intention - whether it's for debug representations, user-visible representations (at which point you need to ask yourself about locales etc). Adding another meaning doesn't seem like a good idea to me.