Re: Overriding Generics



Daniel Dyer wrote:
On Thu, 31 May 2007 20:06:36 +0100, Bryan <BTRichardson@xxxxxxxxx> wrote:

Does anyone know why one can't override the following method

public List<Foo> getFooList()

with something like this

public List<Bar> getFooList()

if Bar extends Foo?

Because List<Bar> is not a sub-type of List<Foo>, even if Bar is a sub-type of Foo.

ArrayList<Foo> is a sub-type of List<Foo>, as is LinkedList<Foo>.

To extend the method as you wish, the super-type method would have to be:

List<? extends Foo> getFooList()

Alternatively, you could make the type containing the method generic:

interface Base<T extends Foo> {
List<T> getFoos();
}

interface Derived extends Base<Bar> {
List<Bar> getFoos();
// (not actually necessary to declare in this case)
}

Tom Hawtin
.