Re: Overriding Generics



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()

Then List<Bar> getFooList() would be fine.

Dan.


--
Daniel Dyer
http//www.uncommons.org
.