0

I have a class Cell and a class Neighbour extending Cell. But I get an error when I try to pass an ArrayList<Neighbour> to a function expecting an ArrayList<Cell>. What have I missed?

class Cell {
    PVector pos;

    Cell(PVector pPos) {
        pos = pPos.get();
    }
}

class Neighbour extends Cell {
    int borders = 0;

    Neighbour(PVector pPos) {
        super(pPos);
    }
}

private int inSet(PVector pPos, ArrayList<Cell> set) {
    [...]

    return -1;
}

[...]

ArrayList<Neighbour> neighbours = new ArrayList<Neighbour>();
PVector pPos = new PVector(0, 0);

[...]

inSet(pPos, neighbours);

The last line throws the error `The method iniSet(PVector, ArrayList) is not applicable for the arguments (PVector, ArrayList);

Thanks for your help!

Lars Ebert
  • 3,487
  • 2
  • 24
  • 46
  • 2
    This may help to understand : http://stackoverflow.com/questions/17131664/inheritance-in-java-with-generics/17131699#17131699 – user2336315 Nov 08 '13 at 07:54

2 Answers2

3

that is because

List<A> != List<B> ... even if B extends A.

What you need to do is modify the function to the following

private int inSet(PVector pPos, ArrayList<? extends Cell> set) {
    [...]
    return -1;
}

Hope that helps.

Harsha R
  • 707
  • 6
  • 12
2

Try with:

private int inSet(PVector pPos, List<? extends Cell> set)
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118