Abhiroop Sarkar
Abhiroop Sarkar

Reputation: 2311

Guava Cache overriding error

Noob question on generics. I was trying to override the loadAll method in the CacheLoader class. Its signature is

public Map<K,V> loadAll(Iterable<? extends K> keys)

now when I override with loadAll(List<Integer>) it throws an error and suggests removing the @Override annotation however the same works with loadAll(Iterable<? extends Integer>). Can someone tell me the difference. Isn't list an iterable too?

Upvotes: 0

Views: 177

Answers (1)

reto
reto

Reputation: 10463

As you can see in the JavaDoc List does implement the interface Iterable.

Your new signature:

public Map<K,V> loadAll(List<Integer>)

however does not override the method:

public Map<K,V> loadAll(Iterable<? extends K> keys)

because you restrict the callers of your method to arguments of type List but to "fullfil the promise" of the interface that you are trying to override you'd have to accept arguments of type Iterable<? extends K> keys. If you provide the interface with the argument type Iterable<? extends Integer> you are doing exactly that.

Another example to clarify this: Your non-working signature wouldn't accept a Set whereas the original method of the interface would.

Upvotes: 2

Related Questions