Java method to accept ArrayLists of different superclasses, must use Interface? -
i have method called findnearest
used find nearest object
point arraylist
of objects.
however want able call method arraylists of different classes superclasses of base class entity
for example tree
subclass of entity
, worker
. want able to findnearest(x,y,<arraylist of trees>)
, findnearest(x,y,<arraylist of workers>)
i think can achieve using interface, there simpler/cleaner way it?
thanks
duncan
you need use covariance:
void findnearest(int x, int y, arraylist<? extends entity> items)
note won't able add list, since don't know type list supposed contain.
Comments
Post a Comment