java - overriding with different return types (generic collections) -



java - overriding with different return types (generic collections) -

please help me solve following:

class cat must extend animal (keep inheritance); animal.getall() must homecoming animals (cats, dogs, etc.) , cat.getall() must homecoming cats (don't alter signature).

i've tried , failed error the homecoming type incompatible animal.getall() on line 8.

class animal { public static list<animal> getall() { homecoming new arraylist<animal>(); } } class cat extends animal { public static list<cat> getall() { homecoming new arraylist<cat>(); } }

change line 2 to:

public static list<? extends animal> getall() {

it work. see java wildcards in generics more information

java generics collections

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -