0

After years of programming in Java I still don't understand this!

The top level class is defined

public abstract class GenericCache<K, T, P> {
  public ArrayList<T> getAll() {}
  public T getSafe(K a_id) {}
}

The subclass is

public class SKUTypeCache<T extends SKUType, P> extends GenericCache<Integer, T, P> {
}

When I try to use it

SKUTypeCache l_cache = getTheCache();
for (SKUType t : l_cache.getAll()) {
}

I get the compile error: "incompatible types: Object cannot be converted to SKUType" on the for loop. Why? I have a similar issue using getSafe().

Mitch
  • 989
  • 1
  • 9
  • 25
  • What is `P` doing? It seems irrelevant. – Sweeper Feb 13 '20 at 18:20
  • P is used in other subclasses of GenericCache, just not this one. – Mitch Feb 13 '20 at 18:24
  • Then it shouldn't be a generic parameter of `GenericCache` or `SKUTypeCache`. Do you agree that your question is a duplicate? – Sweeper Feb 13 '20 at 18:26
  • 1
    `l_cache` is declared as a raw type (`SKUTypeCache`). If you want that `for` loop to work, `l_cache` must be declared as something like `SKUTypeCache` (or whatever type `getTheCache()` is returning). – dnault Feb 13 '20 at 18:35
  • The Raw Type article isn't exactly a duplicate, but it helped me understand what was going on. I changed it to SKUTypeCache extends SKUType, ?> l_cache = getTheCache(); and it works fine now. – Mitch Feb 13 '20 at 19:01

0 Answers0