I’m trying to declare an interface that contains a method which will return a list of things that implement both Comparator<Object> and Action, i.e.
<T extends Comparator<Object> & Action> List<T> getThings();
This compiles fine, but the problem comes when I try to call this method. I want to be able to do this:
List<Action> things = getThings();
List<Comparator<Object>> things = getThings();
When I try to do this I get the following compilation error:
incompatible types; no instance(s) of type variable(s) T exist so that
java.util.List<T> conforms to java.util.List<javax.swing.Action>
found : <T>java.util.List<T>
required: java.util.List<javax.swing.Action>
The following doesn’t work either:
List<? extends Action> things = getThings();
List<? extends Comparator<Object>> things = getThings();
Another way to achieve this effect is to create an empty interface that extends both Comparator<Object> and Action and use that as the return type, i.e.
public interface ComparatorAction extends Comparator<Object>, Action { }
List<ComparatorAction> getThings();
But I don’t want to have to do this. There’s got to be a way to do what I want, right? Any ideas?
Thanks!
P.S. I’m having a tough time coming up with a good title for this post so feel free to change it.
You could also parameterize the method(s) from which you call
getThings(). For instance: