Generics <? super T> and <? extends T>

Asked

Viewed 101 times

4

In the coding below, because the Box<? extends T> extends and the EqualityComparator<? super T> super T? uses so as to make part of the code println true return?

public boolean containsSame
(Box<? extends T> other, EqualityComparator<? super T> comparator){
return comparator.compare(get(),other.get());
}

public interface EqualityComparator<T> {
public boolean compare(T first, T second);
}



Box<Number> nBox = new Box<Number>(42);
Box<Integer> iBox = new Box<Integer>(42);
EqualityComparator<Object> sameObject = new EqualityComparator<Object>() {
public boolean compare(Object o1, Object o2) { return o1 == o2;} 
};
System.out.println(nBox.containsSame(iBox, sameObject));

1 answer

4


Using as a basis that answer, that I even posted in the comments, I can bring to this situation saying the following:

When you declare the method containsSame, thus:

public boolean containsSame(Box<? extends T> other, EqualityComparator<? super T> comparator) {
    //...
}

You’re basically saying that: the parameter other must be a Box of objects of a daughter class of T; and the parameter comparator must be a EqualityComparator object mother-class of T.

With this, you created an object, called nBox, with the guy Box<Number>. That is to say:

public boolean containsSame(Box<? extends Number> other, EqualityComparator<? super Number> comparator) {
    //...
}

In that case, the T will be Number.


Then you called the method containsSame of nBox and passed as a first argument Box<Integer> and as a second argument EqualityComparator<Object>.

Thus, there should be no errors related to data types. Therefore, the relations defined in the parameters are being met by the arguments. That is to say:

  • Integer(the class ?) is a subclass/daughter class of Number (T);
  • And Object (also the class ?) is the superclass/mother class of Number (T), as well as of any other class.

Therefore, the method runs smoothly.

I hope I’ve helped!

  • Thank you very much, the help was immense, I only have one last question, because the Equalitycompator Object needs to be super?

  • In case, you ask why there in the method has want to be <? super T>?

  • Suppose the method comes so, as I would know what to put to run the println smoothly? java public boolean containsSame(Box<T> other, EqualityComparator<T> comparator) {&#xA; //...&#xA;}

  • When you created the object nBox in the example, you defined it as being a Box<Number>. In the definition of your class, you must have placed next to its name the notation <T>. That one T becomes a kind of "nickname" for any class that is passed. In this case, T becomes a "nickname" for Number. Then, in that case, wherever T, you’ll consider it as if you had Number there.

  • 1

    That is, when you have created your object, where it had T, you’ll consider it as if you had Number in place.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.