There's no particular reason I want to do this - I'm just wondering if it is possible. If it helps, here's a fictional situation in which it could be used:
Imagine a type of Enum which is used as a read-only data source, such that each value of the Enum contains distinct content. The Enum implements Readable. Now, suppose we want a method that reads all values of the Enum into a single buffer. That could be implemented as a static utility method in a helper class (see below).
public class ReadableEnumUtils {
/** reads data from all enum values into the charbuffer */
public static <T extends Enum<T> & Readable> int readAll(Class<T> clazz, CharBuffer cb) throws IOException {
int total = 0;
for (T e : clazz.getEnumConstants()) {
int intermediate = e.read(cb);
if (intermediate < 0) {
throw new IllegalArgumentException("The enum value \'" + e.name() + "\' had no data to read.");
}
total += intermediate;
}
return total;
}
}
Preferably, that method would be declared in an interface, but that could be confusing, since it wouldn't be immediately obvious that non-Enum classes should not implement such a method. Ideally, the interface could be defined in such a way that the compiler would ensure it was only implemented by subclasses of Enum. Here's an example of what that interface could possibly look like:
interface ReadableEnum extends Readable {
int read(CharBuffer cb) throws IOException;
int readAll(CharBuffer cb) throws IOException;
}
I don't think it's possible to make the compiler ensure that ReadableEnum is only implemented by subclasses of Enum - is that correct?