0

Here is example from "Java Philosophy" book (remember that FancyToy extends Toy):

Class<FancyToy> ftClass = FancyToy.class;
Class<? super FancyToy> up = ftClass.getSuperclass(); //compiles
Class<Toy> up = ftClass.getSuperclass(); //doesn't compile 

Tried it in IDEA a lot of times, just have an "Incompatible types" message. Please, give me a hint, why we can't use generic type Toy only?

Thank you

Robert Gurjiev
  • 500
  • 1
  • 4
  • 17

1 Answers1

1

The direct superclass of FancyToy might be another class!

Your second line says that: it should be some class that FancyToy extends.

Whereas your third line says exactly which class is required. And either there is actually a level between Toy and FancyToy, or the compiler doesn't do the necessary type inference here.

GhostCat
  • 137,827
  • 25
  • 176
  • 248