0

I have a method with List as a parameter.

private static void setData(List<IData> list) {

}

The interface IData is

public interface IData {
    int getData();
}

And i have a class named Data which implements the interface of IData

public class Data implements IData{

    @Override
    public int getData() {
        // TODO Auto-generated method stub
        return 0;
    }

}

The question is when I use the parameter of List to call method, it fails to compile.

public class Test {
    public static void main(String[] args) {
        List<Data> temp = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            temp.add(new Data());
        }
        setData(temp);

        List<IData> temp2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            temp2.add(new Data());
        }
        setData(temp2);
    }

    private static void setData(List<IData> list) {

    }
}

setData(temp) is error. setData(temp2) is ok. Both of the lists have same content. I don't understand.

SkrewEverything
  • 2,393
  • 1
  • 19
  • 50
tongmutou
  • 55
  • 5
  • 1
    Use an [upper bound](https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html), i.e. `setData(List extends IData> list)` – TNT Mar 14 '17 at 04:13
  • You could use `private static void setData(List extends IData> list) {`, but this might have ramifications to your implementation, personally, I'd just change `List` to `List` – MadProgrammer Mar 14 '17 at 04:14
  • Upper bounds are most useful to people who have obtained the necessary foundation in generics, inheritance, and subtyping. Even with that foundation they are subtle beasts. – Lew Bloch Mar 14 '17 at 04:26

1 Answers1

1

You can only put subtypes of parameters in as arguments, because subtypes establish the is-a relationship: an instance of a subtype is-an instance of the supertypes also. It doesn't matter with your lists what the contents are, only what the list types are, relative to the parameter type expected. Your method setData(List<IData> list) requires an argument that is-a List<IData>, but you tried to pass it a List<Data>, which is not a subtype of List<IData>. Therefore the compiler could not accept the code.

Generics, inheritance, and subtypes are the topic of the Java Tutorial section entitled "Generics, Inheritance, and Subtypes", which I commend to your attention as a good introduction to the topic. Deeper knowledge will be gained by perusal of Angelika Langer's​ excellent web site pertaining to generics, which I also commend to your attention.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10