Reputation: 3795
I'm trying to write a function in three classes that will compare two lists of objects of that same class.
General Idea
List<Toads> aTList = ...
List<Toads> bTList = ...
List<Toads> tResult = compare(aTList , bTList );
List<Frogs> aFList = ...
List<Frogs> bFList = ...
List<Frogs> fResult = compare(aFList , bFList );
Note: Toad
and Frog
can both extend a interface that provides necessary funcitons to compare
The issue is that after comparing I would like to use the returned lists (tResult
and fResult
) with their specific type (Toad
and Frog
).
I tried something like specified here Java interfaces and return types, but I couldn't get all the types to line up.
I also tried using things like <? extends Amphibian>
as the parameter and return type to compare
, but then the returned type is Amphibian
.
Upvotes: 1
Views: 200
Reputation: 9401
Perhaps something this is what you're looking for?
public class AmphibianComparer<T extends Amphibian> {
public List<T> compare(List<T> listOne, List<T> listTwo) {
...
}
}
Upvotes: 2
Reputation: 8884
You don't actually give the signature for compare
. Is this what you want?
<T extends YourInterface> T compare(List<T>, List<T>)
Upvotes: 1
Reputation: 22332
public <T extends Amphibian> List<T> compare(List<T> a, List<T> b);
Upvotes: 1