Problem space has been stripped down.
So I have the following interface that I cannot change.
public interface ItemInterface {
Set<Enum<?>> getItems();
}
I have created the Items enum class
public enum Item{
ITEM1, ITEM2, ITEM3
}
So when I implement the method It does not compile.
public class ItemImpl implements ItemInterface {
@Override
public Set<Enum<?>> getItems() {
Set<Item> vals = new HashSet<>();
vals.add(Item.ITEM1);
return vals;
}
}
The return type does not match and I get compile errors? Any ideas?
Simply declare the vals
as Set<Enum<?>>
:
public class ItemImpl implements ItemInterface {
@Override
public Set<Enum<?>> getItems() {
Set<Enum<?>> vals = new HashSet<>();
vals.add(Item.ITEM1);
return vals;
}
}
How about this, I have changed the interface, coz simply its makes a bit readable. If your interface is part of any other package, @Lino answer can be used
public interface ItemInterface {
Set<? extends Enum> getItems();
}
public enum Item implements ItemInterface{
ITEM1, ITEM2, ITEM3;
@Override
public Set<? extends Enum> getItems() {
Set<Item> as = new HashSet<>();
as.add(ITEM3);
return as;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.