I'm trying to create an abstract method in a abstract class that takes my own Enum as argument. But I want also that that Enum will be generic.
So I declared it like that:
public abstract <T extends Enum<T>> void test(Enum<T> command);
In the implementation, I have en enum as that one:
public enum PerspectiveCommands { PERSPECTIVE }
and the method declaration becomes:
@Override public <PerspectiveCommands extends Enum<PerspectiveCommands>> void test(Enum<PerspectiveCommands> command) { }
But if I do:
@Override public <PerspectiveCommands extends Enum<PerspectiveCommands>> void test(Enum<PerspectiveCommands> command) { if(command == PerspectiveCommands.PERSPECTIVE){ //do something } }
I don't have access to the PerspectiveCommands.PERSPECTIVE
with the error:
cannot find symbol symbol: variable PERSPECTIVE location: class Enum<PerspectiveCommands> where PerspectiveCommands is a type-variable: PerspectiveCommands extends Enum<PerspectiveCommands> declared in method <PerspectiveCommands>test(Enum<PerspectiveCommands>)
I've made a workaround like this:
public <T extends Enum<T>> byte[] executeCommand(Enum<T> command) throws Exception{ return executeCommand(command.name()); } @Override protected byte[] executeCommand(String e) throws Exception{ switch(PerspectiveCommands.valueOf(e)){ case PERSPECTIVE: return executeCommand(getPerspectiveCommandArray()); default: return null; } }
But I would like to know if it's possible to not pass by my workaround?
Java enums will be enhanced with generics support and with the ability to add methods to individual items, a new JEP shows. Since both features can be delivered with the same code change, they are bundled together in the same JEP. The change only affects the Java compiler, and therefore no runtime changes are needed.
Java Enum and Interface As we have learned, we cannot inherit enum classes in Java. However, enum classes can implement interfaces.
I must answer a resounding no because actually you can't. Enums have their own data type and each enum is essentially a new data type.
The enums are type-safe means that an enum has its own namespace, we can't assign any other value other than specified in enum constants. Typesafe enums are introduced in Java 1.5 Version. Additionally, an enum is a reference type, which means that it behaves more like a class or an interface.
In your method implementation PerspectiveCommands
is not the enum but your type parameter, which often is called T
. It thus shadows the enum of the same name like axtavt already said, thus PERSPECTIVE
is unknown here.
Your abstract method declaration is fine but you might use a slightly different approach.
public void test(PerspectiveCommands command)
would not work, since this method would not override the generic version. The reason is that with the generic version the type is inferred from the parameter and thus you could pass any enum.
However, I assume you have an interface or abstract class which defines the abstract method. So try something like this:
interface TestInterface<T extends Enum<T>> { public abstract void test(T command); } class TestImpl implements TestInterface<PerspectiveCommands> { @Override public void test(PerspectiveCommands command) { if(command == PerspectiveCommands.PERSPECTIVE){ //do something } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With