Well this is forbidden, but let me point why:
Let's consider:
FavoritesList<Person> l = new FavoritesList<Contact>();
There are operations that are allowed for FavoritesList<Person>
but forbidden for FavoritesList<Contact>
namely addition of any subclass of Person
which breaks contract for FavoritesList<Contact>
.
What you may be looking for is:
FavoritesList<? extends Person> wildcardedList = new FavoritesList<Contact>();
which means: this is a list of some unspecified type ?
, all elements in this list are of this type ?
, and we know that this type ?
is extending person. Beware that this type wildcards may be unintuitive at first. Basically what they give you is a read-only view of this list.
Lets assume:
class FavoritesList<T>{
void add(T t){...}
}
Basically you can't call:
wildcardedList.add(new Contact());
nor:
wildcardedList.add(new Contact());
because we don't know whether Person
or Contact
is of unspecified type T.
To do that you'd have add wildcard to type of add
parameter, and then it get's messy.