Java generics bounded types with list

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!
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
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!

It means you are most likely working it wrongly in this case. If you are already using generics, you should try and avoid casting as much as possible.

Without considering generics, the following statement is wrong isn't it ?
Code:
List<T>.add((T) new Model());

//Removing generics becomes
List.add(...)
What is List ? It's a type, are you calling a static method of List ?
I would believe it should be
Code:
models.add(...)

In your generics, your hint is "T extends Model" hence you are saying that type "T" is a subclass of type "Model". When you instantiate an object from the Model class/type, how can you say it MUST be of type "T" ?

If it is not of type "T", how can you push into a list that you have declared to only contain objects of type "T" and nothing but type "T" in this statement ?
Code:
private List<T> models;

Your code doesn't make sense, please read the one below.
Code:
class Model {
}

public class ClassA<T extends Model> {
    private List<T> models;

    public ClassA() {
    }

    public List<T> getModels() {
        return models;
    }

    public void add(T model) {
      models.add(model);
    }
}
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top