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

Abstract class doesn't allow inheritance of methods with throw conflicts.

XMLWordPrintable

    • beta
    • generic, sparc
    • solaris_2.5, solaris_2.5.1
    • Verified


      allan.jacobs@Eng 1997-03-31


      In the code my21.java below, the abstract class Y21 does not allow
      inheritance from the two interfaces I1 and I2. These interfaces both
      declare an abstract method f() returning an int value. The two
      methods have throws clauses that are not identical (though they are
      not incompatible). Oddly, if the order of the interfaces in the
      implements clause of the class declaration is reversed, then javac
      allows the construction. This is done in the source code
      my12.java.


      algol% cat my21.java
      interface I1 {
        public abstract int f() throws RuntimeException;
      }
      interface I2 {
        public abstract int f() throws Throwable;
      }
      // Note the order of implementation of I1 & I2
      abstract class Y21 implements I2,I1 {}
      class Z21 extends Y21 {
        public int f() throws RuntimeException {
      RuntimeException e = new RuntimeException();
          throw e;
        }
      }
      class X {
         public static void main(String[] arg)
          {
              Z21 z21 = new Z21();
          }
      }
      algol% javac my21.java
      my21.java:6: Invalid exception class java.lang.Throwable in throws clause. The exception must be a subclass of an exception thrown by int f() from interface I1.
        public abstract int f() throws Throwable;
                            ^
      1 error
      algol% cat my12.java
      interface I1 {
        public abstract int f() throws RuntimeException;
      }
      interface I2 {
        public abstract int f() throws Throwable;
      }
      // Note the order of implementation of I1 & I2
      abstract class Y12 implements I1,I2 {}
      class Z12 extends Y12 {
        public int f() throws RuntimeException {
              RuntimeException e = new RuntimeException();
          throw e;
        }
      }
      class X {
         public static void main(String[] arg)
          {
              Z12 z12 = new Z12();
          }
      }
      algol% javac my12.java
      algol%

            gafter Neal Gafter (Inactive)
            ajacobssunw Allan Jacobs (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: