Date: Thu, 14 Sep 1995 07:04:24 -0400
From: "John D. Ramsdell" <###@###.###>
Subject: Beta interface blues
To: java-interest@java
Cc: ###@###.###
While updating a program to the Beta level syntax, I ran into a
problem with interfaces. I've constructed a general package for
manipulating sets of elements. The elements of the set have the
requirement that they be ordered and testable for sameness. There is
no requirement that elements inherit from a common object other than
Object. In a version of sets for Alpha3, these requirements were
defined using the following interface:
------------
package set;
public interface Element {
boolean same(Element e);
boolean less(Element e); // True when comparable and less than
}
------------
Since a set can be an element of a set, the class Set implements an
Element.
------------
package set;
public final class Set implements Element {
private Pair p; // Store elements in a list of pairs.
....
public boolean same(Element e) {
return e != null
&& e instanceof Set
&& pair_same(p, ((Set)e).p); // <- Cast in question
}
private boolean pair_same(Pair p0, Pair p1) { .. }
public boolean less(Element e) { ... }
}
-----------
The beta Java compiler rejects this code claiming it is illegal to
cast an element to a set. One solution is to define Element as an
abstract class and require that all elements inherit from this class.
However, I would like to allow sets of AppletElements, which inherit
from Applet, simply by extending the Applet class with same and less
methods. My reading of The Java Language Specification suggests that
the cast should be allowed. Is this a bug? If not, how does one
construct a general set package?
John
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to ###@###.###
From: "John D. Ramsdell" <###@###.###>
Subject: Beta interface blues
To: java-interest@java
Cc: ###@###.###
While updating a program to the Beta level syntax, I ran into a
problem with interfaces. I've constructed a general package for
manipulating sets of elements. The elements of the set have the
requirement that they be ordered and testable for sameness. There is
no requirement that elements inherit from a common object other than
Object. In a version of sets for Alpha3, these requirements were
defined using the following interface:
------------
package set;
public interface Element {
boolean same(Element e);
boolean less(Element e); // True when comparable and less than
}
------------
Since a set can be an element of a set, the class Set implements an
Element.
------------
package set;
public final class Set implements Element {
private Pair p; // Store elements in a list of pairs.
....
public boolean same(Element e) {
return e != null
&& e instanceof Set
&& pair_same(p, ((Set)e).p); // <- Cast in question
}
private boolean pair_same(Pair p0, Pair p1) { .. }
public boolean less(Element e) { ... }
}
-----------
The beta Java compiler rejects this code claiming it is illegal to
cast an element to a set. One solution is to define Element as an
abstract class and require that all elements inherit from this class.
However, I would like to allow sets of AppletElements, which inherit
from Applet, simply by extending the Applet class with same and less
methods. My reading of The Java Language Specification suggests that
the cast should be allowed. Is this a bug? If not, how does one
construct a general set package?
John
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to ###@###.###