java - Why can't I add SuperType to List<T> where <T extends SuperType>? -
with
public class supertype { }
and
public class testclass<t extends supertype > { public void dosomething() { list<t> list = new arraylist<t>(); list.add(new supertype ()); // error } }
it won't compile, giving me
the method add(t) in type list not applicable arguments (supertype)
but why?
assume have
public class supertype { } public class subtype extends supertype { }
and use testclass<subtype>
code becomes
public class testclass<subtype> { public void dosomething() { list<subtype> list = new arraylist<subtype>(); list.add(new supertype ()); } }
that has error because new supertype()
not specific enough. has @ least subtype
.
Comments
Post a Comment