I don't see my mistake and don't understand why it doesn't work. I have following class:
public class Generic<K extends Comparable<K>> implements Comparable<Generic<K>> {
// code
public static List<Generic<String>> factory1(){...}
public static List<Generic<Integer>> factory2(){...}
private K mKey;
@Override
public int compareTo(NoteGroup<K> another) {
return mKey.compareTo(another.mKey);
}
}
In another class I don't care about the generic type and would like to use a wildcard like:
public class Anothter {
private List<Generic<? extends Comparable>> mList;
private void method1() {
mList = Generic.factory1();
}
private void method2() {
mList = Generic.factory2();
}
}
But this does not work. The error message is, that a type extending Comparable is expected and not a String / Integer.
Can I solve my issue? Why can't I compile code snippet the above?
Edit: The error is in method1() and method2(). I cannot assign the result of the factory methods to the field mList. The types are incompatible.