public class Test {
public static class Data{
List<? extends AbsModel> definitions;
public Data(List <? extends AbsModel> definitions) {
this.definitions = definitions;
}
public List<? extends AbsModel> getDefinitions() {
return this.definitions;
}
}
public Data getData() {
//code to create different model defnitions that extends MainModel
ModelDef model1 = new ModelDef();
ModelDef2 model2 = new ModelDef2();
final List<AbsModel> createdDefs = new ArrayList<>(); // what should ideally be the generic type here?
createdDefs.add(model1);
createdDefs.add(model2); // class cast exception
Test.Result r = new Test.Result(createdDefs);
return r;
}
}
Below is how the inheritance model look like
class ModelDef extends MainModel{
}
class MainModel extends AbsModel{
}
class ModelDef2 extends AbsMainModel{
}
public abstract class AbsMainModel extends AbsNestableMainModel{
}
public abstract class AbsNestableMainModel extends AbsModel {
}
There will be modeldef and modeldef2 created in getData() method. I want to add these created definition objects to the createdDef list and return the result object.
Can anyone tell me how can I do that?