1

I have these three interfaces in my code:

public interface ParentInterface {
  // some properties and methods
}
public interface ChildOne extends ParentInterface{
  //some extra properties and methods
}
public interface ChildTwo extends ParentInterface{
  //some extra properties and methods
}

I have some function void someFunc(List<ParentInterface>), which should be able to accept hybrid lists with instances implementing either ChildOne or ChildTwo. But what if I have a List<ChildOne> ? is there a better way to call someFunc than this one ?

List<ChildOne> coList = new HashList<ChildOne>();
//add some items to coList
List<ParentInterface> piList = new HashList<ParentInterface>(coList.size());
piList.addAll(coList);
someFunc(piList);

Can't I just make the call like this someFunc(coList) without having to create a new list ?

Younes Regaieg
  • 4,156
  • 2
  • 21
  • 37

1 Answers1

1

As Jon Skeet suggested in a comment, I changed the signature of my function to become void someFunc(List<? extends ParentInterface>) and that worked like a charm !

Younes Regaieg
  • 4,156
  • 2
  • 21
  • 37