I'm referring to the paradigm in Item 34 in Effective Java by Joshua Bloch. I would like to take the method he's using which is to have each related enum
implement a base interface, and initialize an EnumMap
from the "sub-enums." See the code section below. I'm getting a syntax error which I don't understand. I'm not set on this method of implementation, but I would like to understand why it won't work.
Note that this example assumes each class definition is in its own file.
public interface BaseEnum {
...
}
public enum EnumOps1 implements BaseEnum {
...
}
public class Widget {
public Widget() {
regMap = new EnumMap<EnumOps1, WidgetData>(EnumOps1.class);
for (EnumOps1 op : EnumOps1.values()) {
regMap.put(op, getWidgetData(op.key())); // line with syntax error
}
}
protected Map<? extends BaseEnum, WidgetData> regMap;
}
Syntax error detail:
method put in interface java.util.Map
<K,V>
cannot be applied to given types
required: capture#1 of ? extends BaseEnum, WidgetData
found: EnumOps1, WidgetData