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

compiler incorrectly rejects interface int clone();

XMLWordPrintable

    • hopper
    • generic
    • generic
    • Verified



      Name: bsC130419 Date: 07/13/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

      According to JLS2 9.2, interfaces implicitly implement only the PUBLIC methods
      of Object. As clone is protected, it is therefore legal (although somewhat
      pointless), to redeclare it in an interface to have a different return type or
      incompatible throws clause.

      $ cat Foo.java
      interface Foo {
        int clone();
      }
      $ javac Foo.java
      Foo.java:2: clone() in Foo cannot override clone() in java.lang.Object;
      attempting to use incompatible return type
      found : int
      required: java.lang.Object
        int clone();
            ^
      1 error

      Here is the conversation I had with Gilad on the matter:

      -----Original Message-----
        From: Gilad Bracha
      Sent: 13 July 2001 19:06
        To: Eric Blake
        Subject: RE: Questions on interfaces vs. Object

      >Since you clarified that interfaces do not implicitly declare, nor do they
      >inherit, non-public members of Object, what happens to interfaces that
      >declare clone() with an unimplementable throws clause? My assumption is
      >that interface I below should be illegal, because while it is possible to
      >implement I with a lesser throws clause, it is impossible to implement it
      >with the given throws clause. finalize() does not suffer from this fate, as
      >Object declares it as throwing Throwable, and there are no other protected
      >methods to worry about.
      >
      >Currently, javac compiles I, but jikes rejects it.
      >
      >interface I { // is this illegal?
      > Object clone() throws java.io.IOException;
      >}
      >class Bar implements I { // always illegal
      > public Object clone() throws java.io.IOException { return new Bar(); }
      >}
      >class Foo implements I { // legal, if I is legal, because lesser throws
      >clause is also compatible with Object's throws clause
      > public Object clone() { return new Foo(); }
      >}

      I is legal. It may be pointless, but as a matter of principle, I has no
      knowledge of the protected members of Object, so there is no constraint on
      the clone method in an interface. For taht matter:

      interface Broken {

         int clone();
      }

      is also legal, though it is completely useless.


      Cheers, Gilad

      *********************************************
      Gilad Bracha
      Computational Theologist
      Sun Java Software
      http://java.sun.com/people/gbracha/
      (Review ID: 128071)
      ======================================================================

      Name: bsT130419 Date: 10/19/2001


      java version "1.3.0_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_02)
      Java HotSpot(TM) Client VM (build 1.3.0_02, mixed mode)

      public class Class1 implements GenericInterface {}

      public interface GenericInterface {}

      public interface A {
        GenericInterface getSomeValue();
      }

      public interface B extends A {
        Class1 getSomeValue();
      }

      The above will generate an error when attempting to compile B. This doesn't
      seem correct, as Class1 can be considered a GenericInterface.

      This problem specifically arises when two different types of remote references
      can be returned (using J2EE) from a method call. Both of the remotes will
      return the same type of ValueObject when calling getValueObject(). We tried to
      have the remotes implement an additional interface, namely ValueObjectable,
      instead of defining a new interface per pair of possible remotes. That is, a
      generic ValueObject getValueObject() in the interface that could be used by any
      remotes that will be of indeterminate type at runtime.
      (Review ID: 134099)
      ======================================================================

            gafter Neal Gafter (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: