Suppose we have:
public class Parent{
public void setXXX(String XXX);
public String getXXX();
}
public class Children extends Parent{
....
}
Now I want to create a clone List method, as shown below:
public List<Something> cloneList(List<Something> original){
List<Something> newList=new ArrayList<>();
for(Something e:original){
Something newE=new Something();
newE.setXXX(e.getXXX());
newList.add(newE);
}
return newList;
}
We want cloneList to apply to List<Parent>both and to List<Children>, anyway, which applies to "Something"?
Something can't be "?" parent or parent since Java is Collection<Parent>not compatible withCollection<Children>
Assumption: 1. Do not want to use any serialization approach or reflection.
We cannot change the Parent and Child class. This is predefined in a third-party bank.
The SuperParent class is not possible, because we cannot modify Parent, as indicated in 2.
source
share