java - Generics to prevent Parent class to be as accepted type -
i have situation in have create generic method can accept list of child objects not parent class objects.
say have
class classa {}
which getting extended by
class classb extends classa {} class classc extends classa {}
executemethod(list</* either classb or classc not classa */>)
can achieve using generics?
i'm not sure why can't allow list<? extends classa>
try:
<t extends classa> void executemethod(list<? extends t>); //not sure if work worth try.
or
public interface classx {} public class classa { void executemethod(list<? extends classx); } public classb extends classa implements classx {} public classc extends classa implements classx {}
Comments
Post a Comment