1

I understand why I can't cast one class that generelized with A, to same class that generelized with B (if B extends A). It's explained here. But I have no clue why this code doesn't work.

public class A {

    public static void main(String[] args) {
        D<? extends B> d = new D<>();
        d.foo(new B()); // compilation error
        d.foo(new C()); // compilation error
    }
}

class B {

}

class C extends B {

}

class D<T> {
  void foo(T t) {

  }
}

I would like to have the reference to official docs where this principle is explained.

deathangel908
  • 8,601
  • 8
  • 47
  • 81

0 Answers0