0

I am begining to believe it is not possible to extend EnumMap in Java because the super constructor is problematic. A super() constructor must be called.

I have trialed several variations on the call to super(...) below. They all give syntax errors.

  • Is there a solution that will work as indicated below?

Intented:

 abstract public class EnumModel<K extends Enum<K>, T>  extends EnumMap<K, T>
{

    protected EnumModel( ) {

       super( K.class );    //  <-- "Cannot select from a type variable"
    }
}

For contrast, the example below with a specific Enum type will compile. But only for Enum Colour.

enum Colour{
   RED, BLACK, BLUE;
}

abstract public class EnumModel<T>  extends EnumMap<Colour, T>
{

    protected EnumModel( ) {

       super( Colour.class );    //  <-- will compile
    }
}
will
  • 4,799
  • 8
  • 54
  • 90
  • There's no specific issue with `EnumMap` here. The problem is that `K.class` is a compiler error. You either have to pass the `Class` in to the constructor, or *sometimes* you can use reflection. There are examples of both at the linked Q&A https://stackoverflow.com/q/3437897/2891664. – Radiodef Jul 27 '18 at 01:39
  • @Radiodef ... I was unaware of that question. I found passing the class works for an EnumMap constructor. I am not so sure this is a duplicat Question. It is solved by some of the answers to the other question. This a refinement or edge-case of the Generics problem in the other Q&A.. – will Jul 27 '18 at 02:27

0 Answers0