I am hoping to produce some base classes that will include something like the following
class Container<T extends Containee>
{
T containee;
public T getContents(){return containee;}
public void setContents(T contents){ containee = contents; containee.setContainer(this); }
}
class Containee<T extends Container>
{
T container;
public T getContainer(){return container;}
public void setContainer(T container){this.container = container;}
}
There is a circular definition problem here which I thought I could ease using wildcards
class Container<T extends Containee<?>>
class Containee<T extends Container<?>>
except that the compiler complains about containee.setContainer(this)
. I have hacked a couple of ad-hoc solutions that will get the super-classes to compile, but nothing that will work for sub-classes, e.g.
class Foo extends Container<Bar>
class Bar extends Containee<Foo>
The generics tutorial and FAQ didn't seem to have anything obvious relating to this. How does one express such a relationship using generics?
Thanks