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 because the covariant return data types do not seem to work with enum constants (again breaking the illusion that enum constants are singleton subclasses of an enum type ...) So, how about adding a few 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, but the individual constants are "more specific" than the type of the enumeration as a whole ...
source share