Is there a way to provide a default toString() to an interface?

Wang Weijun at
Wed Mar 12 07:45:33 UTC 2014

Hi All

I have an interface that wraps an integer, like this

  interface Wrapper {
    int getX();

Why cannot I add a default toString method

    default String toString() {
      return "This is " + getX();

The error is

error: default method toString in interface DSAPublicKey overrides a member of java.lang.Object
    default String toString() {

Is there any other way to do it? I do not want to rename it to getString() and let all child classes call it in their toString().


P.S. security-dev@ guys, I want to move toString() back to {DSA|RSA|EC}PublicKey so that we get the same strings for SunPKCS11 and SunJCE.

More information about the compiler-dev mailing list