Reputation: 121
Consider having a generic type:
public interface <T> Assigner {
<S> S assign(T type);
}
Now, let's say I want to have different types of assigners that could assign an object of any type T
to a group of any type S
. Example:
interface PlayerAssigner extends Assigner<Player> {
<S> S assign(Player player);
}
interface ManagerAssigner extends Assigner<Manager> {
<S> S assign(Manager manager);
}
...
I would also like to introduce a group-type-based assigner, but this is where I got stuck. I tried:
interface CityBasedAssigner<T> extends Assigner<T> {
City assign(T type);
}
, but this won't work, it says that both methods have the same erasure. I don't know whether it should be an abstract class or something different, or whether the whole concept could be simplified. What I want is to be able to instantiate different types of assigners from the combination of two type parameters, e.g. CityBasedPlayerAssigner, CountryBasedPlayerAssigner, CountryBasedManagerAssigner, etc., and this would have been the next step: combining the two extended interfaces and create many implementations based on these two.
What am I missing here?
Upvotes: 0
Views: 67
Reputation: 1493
Your sub interface inherits the method assign
from Assigner
. All you need to do in the sub interface is pass the types to the super interface.
interface Assigner<S, T> { S assign(T type);}
interface PlayerAssigner<S> extends Assigner<S,Player> {}
interface ManagerAssigner<S> extends Assigner<S,Manager> {}
interface CityBasedAssigner<T> extends Assigner<City,T> {}
Upvotes: 2