Why is Predicate<? super SomeClass> not applicable to Object?

  • A+
Category:Languages

Assume we have a predicate declared as Predicate<? super SomeClass>. I would naively expect it to be applicable to any superclass of SomeClass up the hierarchy, including Object.

However this predicate is not applicable to Object. I get the following error:

The method test(capture#3-of ? super SomeClass) in the type Predicate is not applicable for the arguments (Object)

Demo.

Why is Predicate<? super SomeClass> not applicable to an instance of Object?

The code:

import java.util.*; import java.lang.*; import java.io.*; import java.net.URL; import java.util.function.Predicate;   /* Name of the class has to be "Main" only if the class is public. */ class Ideone {     public static void main (String[] args) throws java.lang.Exception     {         Predicate<? super URL> p = u -> u.getFile().isEmpty();         p.test(new Object());     } } 


For a Predicate<? super SomeClass> variable, you can assign a Predicate<SomeClass> instance, or a Predicate<Object> instance.

However, you can't pass an Object to the test() method of a Predicate<SomeClass>. You can only pass a SomeClass instance.

Therefore you can't pass an Object to the test() method of a Predicate<? super SomeClass>

Consider the following:

Predicate<URL> p1 = u -> u.getFile().isEmpty(); Predicate<? super URL> p2 = p1; 

p2 is referring to a Predicate<URL>, so you can't pass a new Object() to its test() method.

In other words, in order for p.test(new Object()) to be accepted by the compiler, it must be valid for any Predicate that can be assigned to the Predicate<? super URL> p variable. Since the Predicate<URL> Predicate can be assigned to that variable, and its test() method cannot accept an Object, p.test(new Object()) cannot be accepted by the compiler.

BTW, in your specific example, you are creating a Predicate<URL>, and URL is a final class. Therefore, you should simply declare it as:

Predicate<URL> p = u -> u.getFile().isEmpty(); 

There's no reason for ? super or ? extends.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: