Having Container<? extends Element> Container<? extends Element> means that the Container can < express Element (s), but cannot consume Element (s) .
The reason for this is what ? extends Element ? extends Element stands for a whole family of unknown Element subtypes. Suppose you set your container to Container<SomeSubElement> . Then passing this to the container (even you know that Element or a subtype of Element ) will be wrong, because this may or may not be SomeSubElement (depending on the type of Runtime).
In the Generics world, this is called co-dispersion.
To compile the code (I cannot guarantee that you need exactly this), you can do (note that I changed the container to be a consumer of Element (s) instead of the manufacturer)
public class Element { private Container<? super Element> container; public Container<? super Element> getContainer() { return container; } public void setContainer(Container<? super Element> container) { this.container = container; } public void doStuff() { getContainer().doStuff(this); } }
However, if you need your Container be a producer and a consumer at the same time, just get rid of the template and parameterize it only with <Element> .
source share