9

Class<? extends Integer> will compile fine, but Integer is a final type so it doesn't make sense to use it as an upper bound (nothing will ever extend it).

If you try to use a final type as an upper bound for a type parameter, you will get a compiler warning:

The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended

Why would using a final type as an upper bound for a wildcard be perfectly fine, but throw a warning for a type parameter? Why does Java even allow for wildcards to be bounded by a final upper type?

Jeffrey
  • 44,417
  • 8
  • 90
  • 141

1 Answers1

5

Class<Integer> is not as permissive for assignment as Class<? extends Integer>.

For example, this compiles:

Class<? extends Number> numberClass = Integer.class;
Class<? extends Integer> integerClass = numberClass.asSubclass(Integer.class);

This doesn't:

Class<? extends Number> numberClass = Integer.class;
Class<Integer> integerClass = numberClass.asSubclass(Integer.class);

Myself, I couldn't get a compiler warning as you do (perhaps you could provide an example and details on your compiler?).

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • 1
    It seems to me that the compiler could figure out for itself that the `Class extends Integer>` returned from your second example is compatible with `Class` because `Integer` is final. But I suppose that would add more complexity to the compiler than its worth. Maybe the warning is Eclipse specific: Eclipse Juno w/ JDT 3.8.0. – Jeffrey Aug 11 '12 at 22:05