LongBigBronzeElephantFish
Senior Member
- Joined
- Jan 2, 2012
- Messages
- 1,252
- Reaction score
- 0
I got a small problem here regarding generics bounded type with lists. Please help out!
Model.java
public class Model {
}
SubModel.java
public class SubModel extends Model {
}
ClassA.java
public class ClassA<T extends Model> {
private List<T> models;
public ClassA() {
List<T>.add((T) new Model());
}
public List<T> getModels() {
return models;
}
}
It gives me an unchecked cast from Model to T warning on this line:
List<T>.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?
Thanks!
Model.java
public class Model {
}
SubModel.java
public class SubModel extends Model {
}
ClassA.java
public class ClassA<T extends Model> {
private List<T> models;
public ClassA() {
List<T>.add((T) new Model());
}
public List<T> getModels() {
return models;
}
}
It gives me an unchecked cast from Model to T warning on this line:
List<T>.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?
Thanks!