java - overriding with different return types (generic collections) -
java - overriding with different return types (generic collections) -
please help me solve following:
classcat 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
Post a Comment