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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -