I have several Java enums using underscores to separate words. In each of my enums I have a toString method which is implemented as super.toString().replace("_", " ") so that I can display the String values on a GUI without the underscores. Is there some way I can have all my enums use this toString method without duplicating it across all of them? If this were a regular class the answer is simple, implement toString() then have all my classes that need that implementation extend that class, but enums can't extend anything, not even another enum.
public enum Example1 {
HELLO_WORLD, HELLO_EARTH, HELLO_GLOBE
@Override public String toString() {
return super.toString().replace("_", " ");
}
}
public enum Example2 {
ANSWER_1, ANSWER_2, ANSWER_3
@Override public String toString() {
return super.toString().replace("_", " ");
}
}
Does anyone have a solution that doesn't duplicate this toString() method across all enums?