Unchecked Assignment with generic class in Java

65 Views Asked by At

I'm working with generic classes with target to bring polymorphism to my implementation. So at first i have an interface like :

interface IDProvider {int getID();}

Which guarantee will return id as an integer. So i have another interface :

interface Tracker<T extends IDProvider> {
    List<T> getProviders();
    default List<Integer> getProviderIDs() {
      return getProviders().stream().map(T::getID).collect(Collectors.toList());
    }
}

So the default method should guarantee to return a list of But if i try to use the interface's method:

Tracker tracker = // some implemented instance
List<Integer> ids = tracker.getProviderIDs();

I still get this warning:

Unchecked assignment: 'java.util.List' to 'java.util.List<java.lang.Integer>'. Reason: 'tracker' has raw type, so result of getProviderIDs is erased.

I kinda accept that if i call getProviders method of Tracker interface, the compiler won't figure out which type it will return because it return a List of raw type . But this default method getProviderIDs obviously return a list of integer, so why i still get this warning?

Is there any logical explanation for this, and if so, a proper way to implement those generic classes?

0

There are 0 best solutions below