Is this method head correct? (Bounded type variables in Java)

180 Views Asked by At

In this The Grey Blog tutorial about Java Generics, the author tries to show how to write a method head that uses Generics in such a way as to check whether a parent class of the object that method takes as an argument implements a certain interface.

The following classes are involved:

public interface Juicy<T> {
    Juice<T> squeeze();
}

    class Orange extends Fruit implements Juicy<Orange>;
    class RedOrange extends Orange;

Now we want to "squeeze" a List of red oranges using the squeeze method that only the class Orange implements:

<T extends Juicy<? super T>> List<Juice<? super T>> squeezeSuperExtends(List<? extends T> fruits);

At the beginning <T extends Juicy<? super T>> declares that T must either directly implement the interface Juicy or be subclass of a class that implements Juicy (I hope I am right so far?)

But then, when the method declares its argument types (List<? extends T> fruits) it again declares that the List can contain objects ob T or of any of Ts subclasses.

Wouldn't it be enough to write either the first part:

<T extends Juicy<T>> List<Juice<? super T>> squeezeSuperExtends2(List<? extends T> fruits)

or the last part:

<T extends Juicy<? super T>> List<Juice<? super T>> squeezeSuperExtends3(List<T> fruits)

Thank you so much!

0

There are 0 best solutions below