I am trying to create a hierarchy of classes, starting with an abstract class with a generic type. And then use an extended type of the generic in the 2nd generation of children classes. Since i'm pretty sure that's unclear, here's an example :
The idea would be to have a table of items, so basically :
abstract class AbstractTableOfItems<T> -> The generic type of table
class Foo -> One type of item
class Bar -> Another type on item
class SonOfFoo extends Foo
class TableOfFoo extends AbstractTableOfItems<Foo>
class TableOfBar extends AbstractTableOfItems<Bar>
So far so good, everything is fine (let's not care about the content of classes, it's not the point here).
But then, I want a table of SonOfFoo and I want it to extend TableOfFoo since SonOfFoo inherits from Foo. This does not seem to be possible. Perhaps I'm thinking the wrong way, but I just don't understand why.
class TableOfSonOfFoo extends TableOfFoo
compiles but is wrong, because it would still be a table of Foo, the generic type used being Foo.
class TableOfSonOfFoo extends TableOfFoo<SonOfFoo>
not does compile, since the generic type is on the AbstractTableOfItems.
So ok I say, let's try to redefine TableOfFoo so it can accept inheritance :
class TableOfFoo extends AbstractTableOfItems<? extends Foo>
wrong again, "A supertype may not specify any wildcard" (I'm not sure I fully understand this one).
I cannot touch AbtractTableOfItems, since it wouldn't be as generic as I want. So I'm kinda stuck here.
I understand it would work by just having TableOfSonOfFoo inherit directly from AbstractTableOfItems, but then I'd lose all the implementations from TableOfFoo.
I could go around this, but I'm trying to think of a hierarchy, and understanding why this won't work.
