Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1255581

Can't override a method with a return type that is assignable to the old one

XMLWordPrintable

    • x86, sparc
    • solaris_2.5, windows_95

      The compiler (1.0.2) rejects code in which a subclass overrides a method with a different return type that is assignable to the overridden method's return type.
      The following code:

      public class test1
      {
      test1 fredfunc() {return null;}
      }

      class test2 extends test1
      {
      test2 fredfunc() {return null;}
      }

      produces the following error from the compiler:

      test1.java:8: Method redefined with different return type: test2 fredfunc() was
      test1 fredfunc()
      test2 fredfunc() {return null;}
                    ^
      1 error

      According to the 1.0 Beta version of the language specification, page 48:

      "The return type of an overriding method must be assignable to the return type of the overridden method."

      Perhaps this has changed in the 1.0.2 version of the language specification, but since the relevant chapter of the newer language specification isn't available, I couldn't check it.

            fyellinsunw Frank Yellin (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: