Interface array in Java
I have an interface a:
interface A { }
Then I have a level B:
class B implements A { }
Then I have a way to use the a list:
void process(ArrayList<A> myList) { }
I want to pass a B list:
ArrayList<B> items = new ArrayList<B>(); items.add(new B()); process(items);
But there is a type mismatch error I understand why ArrayList itself is a type. It has no function of converting from ArrayList < b > Go to ArrayList < a > Is there a quick and resource oriented way to form a new array suitable for the process methods passed to me?
Solution
I think the simplest solution is to change one of the methods to:
void process(ArrayList<? extends A> myList) { }
Note, however, that when using this solution, the entire list must be of the same type That is, if you have a class C that also implements a, you can't mix the items in the array so that some of them belong to class B and some belong to class C
In addition, as noted in the comments below, you will not be able to add objects to the list in this method