0

In one of my classes I have...

public Class<? extends Map<K, V>> getObjectType() {
    return (Class<? extends Map<K, V>>) Map.class;
}

What is wrong with this code

I keep getting

error: incompatible types: Class <Map> cannot be converted to Class<extends Map<K,V>>
    return (Class<? extends Map<K, V>>) Map.class;

Can someone point me in the right direction

koala421
  • 705
  • 2
  • 9
  • 24

1 Answers1

0

K and V aren't actual types and would be erased by type erasure anyway. Just use Map. And there is no need to cast. You only cast to a more specific type and Map.class is an instance of Class<? extends Map>.

public Class<? extends Map> getObjectType() {
    return Map.class;
}

other examples using compatible types:

public Class<? extends Map> getObject1Type() {
    return HashMap.class;
}

public Class<? extends Map> getObject2Type() {
    return LinkedHashMap.class;
}
Novaterata
  • 3,327
  • 22
  • 39