0

Integer extends Number so why do I get the error at the bottom?

interface Predicate<T> {
   public abstract boolean check(T t);
}

Predicate<? extends Number> predUpper = null;

predUpper.check(new Integer(73));

Error: The method check(capture#6-of ? extends Number) in the type Predicate is not applicable for the arguments (Integer)

I have read: Method in the type Map<String,capture#1-of ? extends Object> is not applicable and Difference between <? super T> and <? extends T> in Java

Community
  • 1
  • 1
Stanko
  • 3,209
  • 3
  • 16
  • 45

1 Answers1

6

Since the Predicate will consume the Integer, you should do:

Predicate<? super Integer> predUpper //Solution 1

or just:

Predicate<Number> predUpper //Solution 2

There's difference, however, in these approaches:

  • The type-parameter <? super Integer from "Solition 1" represents a whole family of super-types of Integer (including Integer).

  • The type-parameter <Number> from "Solution 2" represents a sub-class of the Number interface. Since Integer is such, it's applicable here.

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 59,030
  • 8
  • 92
  • 140