Re: Overriding Generics
- From: Tom Hawtin <usenet@xxxxxxxxxxxxxxxxx>
- Date: Thu, 31 May 2007 20:36:46 +0100
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
.
- References:
- Overriding Generics
- From: Bryan
- Re: Overriding Generics
- From: Daniel Dyer
- Overriding Generics
- Prev by Date: Re: DateFormat question
- Next by Date: Re: DateFormat question
- Previous by thread: Re: Overriding Generics
- Next by thread: console parsing
- Index(es):