3

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.

vRallev
  • 4,982
  • 3
  • 31
  • 34
  • What is Generic class? What is exactly error line? – LHA Nov 17 '14 at 15:19
  • 5
    *The error is in method1() and method2(). I cannot assign the result of the factory methods to the field mList. The types are incompatible.* and they will be. You cannot use `Foo extends Bar> bar` and just initialize it as you want. For example, think on `List extends Comparable> comparables = new ArrayList();` but since the list supports any `Comparable` then you can add `String` or any other element that is a `Comparable` there, which the compiler cannot allow for *safety*. – Luiggi Mendoza Nov 17 '14 at 15:27
  • `mList` contains any type of `Comparable`, which would allow any instance of any subclass of `Comparable` to be added to `mList`. – njzk2 Nov 17 '14 at 15:28
  • @SME_Dev that's not a proper solution. – Luiggi Mendoza Nov 17 '14 at 15:31
  • 5
    @SME_Dev please read here: [Is List a subclass of List? Why aren't Java's generics implicitly polymorphic?](http://stackoverflow.com/q/2745265/1065197) – Luiggi Mendoza Nov 17 '14 at 15:34
  • @LuiggiMendoza That doesn't work either. The generic types are still incompatible. – vRallev Nov 17 '14 at 15:35
  • The whole problem is using `Foo extends Whatever> bar = ...` that's just plain wrong and you cannot use it, the compiler just don't understand what `Foo extends Whatever` is. Please read the accepted answer in the link I've provided. In short, you should not declare a variable using that approach. – Luiggi Mendoza Nov 17 '14 at 15:36
  • Compiler does not understand that casting but it does not means we can't cast it at Runtime. – LHA Nov 17 '14 at 15:52
  • 1
    possible duplicate of [Java nested generic type](http://stackoverflow.com/questions/22806202/java-nested-generic-type) – Radiodef Nov 17 '14 at 16:45
  • 2
    The declaration you are looking for is `List extends Generic>> mList;` but note a wildcard will not allow you to make comparisons easily. – Radiodef Nov 17 '14 at 16:45

2 Answers2

1

As said in one of the comments - To make your code work you need to change following declaration in class Another

private List<Generic<? extends Comparable>> mList;

to :

List<? extends Generic<?>> mList;
nanosoft
  • 2,913
  • 4
  • 41
  • 61
0
private void method2() {
    mList = (List) Generic.factory2();
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Hajmola
  • 93
  • 1
  • 6