As mentioned in another question on this site, something like this is not legal:
public enum MyEnum {
FOO {
public Integer doSomething() { return (Integer) super.doSomething(); }
},
BAR {
public String doSomething() { return (String) super.doSomething(); }
};
public Object doSomething();
}
This is due to covariant return types apparently not working on enum constants (again breaking the illusion that enum constants are singleton subclasses of the enum type...) So, how about we add a bit of generics: is this legal?
public enum MyEnum2 {
FOO {
public Class<Integer> doSomething() { return Integer.class; }
},
BAR {
public Class<String> doSomething() { return String.class; }
};
public Class<?> doSomething();
}
Here, all three return Class
objects, yet the individual constants are "more specific" than the enum type as a whole...
Fundamentally, the problem is that the compile-time type of MyEnum.FOO
is MyEnum
, not the specific generated subclass. You can see this without any covariance:
enum MyEnum {
FOO {
public void foo() {}
};
}
public class Test {
public static void main(String[] args) throws Exception
{
MyEnum.FOO.foo(); // Error
}
}
Basically, the compiler will only see the signatures declared in MyEnum
.