Why is List compatible with list <?>, But List <List> is not with list <List <? >>?

I implemented some interfaces with non-generic code, and I wanted to get rid of warnings without @SupressWarnings . But then I found myself in this situation:

 public abstract class A { public abstract List m1(); public abstract List<List> m2(); } public class B extends A { @Override public List<?> m1() { // the compiler is happy with this // whatever } @Override public List<List<?>> m2() { // but not with this // whatever } } 

Why? Why can I change the return type from List to List<?> When overriding a method and cannot apply the same logic from List<List> to List<List<?>> ?

This is the message that the compiler gives me:

The return type java.util.List<java.util.List<?>> not compatible with java.util.List<java.util.List>

But why is List compatible with List<?> And List<List> not with List<List<?>> ?

I made a couple of demos:

+5
source share

Source: https://habr.com/ru/post/1235571/


All Articles