7

I can't grasp what is wrong here

import java.util.*;

class Main {
    private static class SomeX {}

    void doSomethingWithX(SomeX someX) {
        Collection<? extends SomeX> colectionOfX = new ArrayList<>();
        colectionOfX.add(someX);
    }
}

javac says following:

  Main.java:8: error: method add in interface Collection<E> cannot be applied to given types;
          colectionOfX.add(someX);
                      ^
    required: CAP#1
    found: SomeX
    reason: argument mismatch; SomeX cannot be converted to CAP#1
    where E is a type-variable:
      E extends Object declared in interface Collection
    where CAP#1 is a fresh type-variable:
      CAP#1 extends SomeX from capture of ? extends SomeX

To my understanding extends limits the lower bound of CAP#1 to SomeX and SomeX itself should satisfy the limit.

What's wrong? (Compiled with javac 1.8.0_102)

Xtra Coder
  • 3,389
  • 4
  • 40
  • 59

2 Answers2

1

If you want to allow colectionOfX to contain any instance of SomeX, it should be declared as :

Collection<SomeX> colectionOfX = new ArrayList<>();

When you declare it as

Collection<? extends SomeX> colectionOfX = ...;

it means you can assign to it any Collection that holds elements of some type that is either SomeX or a sub-class of SomeX.

For example, you can assign to it a List<SomeBX>, where SomeBX extends SomeX. In that case only instances of SomeBX can be added to the collection. So if you try to add to the collection a SomeX instance which is not a SomeBX (for example, an instance of SomeAX, which is also a sub-class of SomeX), it would be invalid.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    So ... all this does not answer question of "Why I can't add `instance of SomeX` into `Collection extends SomeX>`". What is wrong in that particular operation? – Xtra Coder Sep 25 '16 at 09:04
  • @XtraCoder As I tried to explain, `Collection extends SomeX> colectionOfX` doesn't accept all instances of `SomeX`. Even if you assign to it `new ArrayList()`, to which you should be able to add any instance of `SomeX`, the compiler can't know that, since it only relies on the compile time type, which is `Collection extends SomeX>`. – Eran Sep 25 '16 at 09:14
1

So, the question is actually already answered in "How can I add to List<? extends Number> data structures?", but googling just did not bring that match.

Short answer, as far as I get it, is below:

class Main {
    private static class SomeX {}

    private static class SomeY extends SomeX {}

    void doSomethingWithX(SomeX someX) {
        Collection<? extends SomeX> colectionOfX = new ArrayList<SomeY>();
        colectionOfX.add(someX);
    }
}

Because <? extends SomeX> is rather a lower-bound limit for Collection type than item type - it will be possible to assign instance of collection with higher-bounded value and, taking this into account, putting SomeX into such collection is not type-safe from compiler's perspective.

Community
  • 1
  • 1
Xtra Coder
  • 3,389
  • 4
  • 40
  • 59