0

I am sure that I have a silly bug but I am unable to pass a list of objects that implements an interface to a function that requires a list of that interface.

This is the function signature:

public void add(ArrayList<PairStringInterface> data){ ... }

And this is how I am calling this function:

ArrayList<Sector> list = new ArrayList<Sector>();
add(list);

And this is the class Sector

public class Sector implements PairStringInterface { ... } 

I receive an error that there isn't any suitable function for the parameters

Error:(88, 31) error: no suitable method found for add(ArrayList<Sector>) 
method PairStringSpinnerAdapter.add(ArrayList<PairStringInterface>) 
is not applicable (argument mismatch; ArrayList<Sector> cannot be converted to ArrayList<PairStringInterface>)

If I iterate the list and add a method to receive just one item it works.

public void add(PairStringInterface elem) { ... }

ArrayList<Sector> list = ....
for(Sector s : list){
    add(s);
}
David Rojo
  • 2,337
  • 1
  • 22
  • 44
  • 2
    http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p?rq=1 – GriffeyDog Jul 13 '16 at 18:23

1 Answers1

1

You should use a generic type placeholder, like follow

public <T extends PairStringInterface> void add(ArrayList<T> list) {
    // Do your things here...
}

Changing the method signature as above you should be able to use any ArrayList<Sector> passed as an argument to that very method.

Dan.see
  • 88
  • 11