In Java I have an interface I, a class C and a class D, both implementing I:
interface I
{
//some method signatures
}
class C implements I
{
//implement methods from I
}
class D implements I
{
//implement methods from I
}
Now I create a List holding elements of class C, and a second List holding elements of D:
List<C> c = new LinkedList<C>();
List<D> d = new LinkedList<D>();
I have a method that I want to apply to all Lists that hold elements implementing interface I:
public void modifyList(List<I> l)
{
//call some method defined in I
}
However, when I try to call that function the compiler throws an error:
public void testModification()
{
modifyList(c);
modifyList(d);
}
==> The method modifyList(List<I>) in the type [...] is not applicable for the arguments (List<C>) / (List<D>)
I can get it to work by redefining my List, such that it reads
List<I> c1 = new LinkedList<I>();
But now c1 can hold Elements from both classes C and D, which is something I want to avoid. How can I create a List such that I can define exactly what class' elements it should hold but still use it as a parameter for my function?