I got a small problem here regarding generics bounded type with lists. Please help out!
Model.java
public class Model {
}
ClassA.java
public class ClassA<T extends Model> {
private List<T> models;
public ClassA() {
models.add((T) new Model());
}
}
It gives me an unchecked cast from Model to T warning on this line:
models.add((T) new Model());
I understand I'm getting this warning because all I can safely cast from a sub class into a super class but not the other way round.
Is there any way to get over this problem or can I just safely supress the warning?
models.add(new T());
? – KeiClassCastException
when you try to use the elements in the list asT
type objects whereT
is a subclass ofModel
. – Roughshodnew T()
won't compile due to type erasure. – Eudora