I want to create two interfaces with inverse relationships.
public interface Item <D extends Description,
C extends Category<D,Item<D,C>>> {
public C getCategory();
public void setCategory(C category);}
I’m not sure if expression C extends Category<D,Item<D,C>> is correct, but at least there are no compiler errors.
public interface Category<D extends Description, I extends Item> {
public List<I> getItems();
public void setItems(List<I> items);}
I extends Item gives the warning Item is a raw type. References to Item<D,C> should be parametrized. I tried
I extends Item<D,Category<D,I>>
but this results in the error Bound mismatch: The type Category<D,I> is not a valid substitute for the bounded parameter <C extends Category<D,Item<D,C>>> of the type Item<D,C>. How to I parametrize the interface Category correctly with generics?
This seems to work :). I have no idea how to explain it ( i normally try to avoid doing stuff like that ) but here it goes:
Now if you do this:
the type of the category returned by the
customItem.getCategory()is CustomCategory which i think is what you actually want.