I have the following class & interface defined:
public interface A {
}
public class B implements A {
}
I have a List
of <
We can achieve this using Generics and Wildcards. You might not be able to create List
but create something like List extends A>
which will call all the methods of the interface which is enough for most I think.
Creating a new collection is expensive and not advisable. Since the references of the same objects will be passed on into the new collection, the additional list creation step is just an overhead.
List bList = new ArrayList();
List extends A> aList = bList;