to program by contract or by interface, I define a class like this:
public ClsOne{
private List<IntfA> fieldA;
public setFieldA(List al);
}
public ClsA implements IntfA{...}
then in somewhere else, we can write:
....
ArrayList<ClsA> alist = new ArrayList<ClsA>();
ClsOne one = new ClsOne();
one.setFieldA(alist);
But, this seems not available for embedded containers:
public ClsTwo{
private List<List<IntfA>> fieldA;
public setFieldA(List<List<IntfA>> al);
}
public ClsA implements IntfA{...}
since the code below is not correct:
ArrayList<ArrayList<ClsA>> aalist = new ArrayList<ArrayList<ClsA>>();
ClsOne one = new ClsOne();
one.setFieldA(aalist);
So, is there any work around to hide concrete container type under this circumstance in ClsA’s definition?
You can instantiate an
ArrayListwhich contains objects of typeList<ClsA>: