Generics super vs. extends

后端 未结 4 840
情书的邮戳
情书的邮戳 2020-12-30 16:23

Just when I thought I finally understood generics, I came across the following example:

public class Organic {
          void react(E e) { }
            


        
相关标签:
4条回答
  • 2020-12-30 16:38

    I think you misunderstood what you set as a parameter in your react() method

    Try changing

    void react(E e) { }
    

    to

    void react(Organic<E> e) { }
    

    to see the difference. You are looking for objects: Organic<E> Aliphetic<E> Hexane<E>

    Not Organic<E> as E

    Nor Aliphetic<E> as E

    Nor Hexane<E> as E

    0 讨论(0)
  • 2020-12-30 16:45

    You made some mix.
    Organic<E> does equal in its type to Organic.
    You cannot perform
    new Organic()); You must at least consider performing
    new Organic<Object>()); And this is just one compile error I can think of
    This of course is valid for your other object instantiations.

    0 讨论(0)
  • 2020-12-30 16:52

    Your first declaration

    Organic<? extends Organic> compound
    

    means that compound could be an Organic<SomeSubtypeOfHexane> (since Aliphatic extends Organic, Hexane extends Aliphatic and SomeSubtypeOfHexane extends Hexane).

    In that case, compound.react(new Organic()), compound.react(new Aliphatic()) and compound.react(new Hexane()) would lead to a type error, since E in compound must be a SomeSubtypeOfHexane (or subtype thereof).


    Your second declaration

    Organic<? super Aliphatic> compound
    

    means that compount could be an Organic<Aliphatic>.

    In that case compound.react(new Organic()) would lead to a type error, since E must be an Aliphatic (or subtype thereof).


    Remember that declaring a variable using A<? extends B> or A<? super B>

    • extends the amount of objects that can be assigned to it, and, in consequence,
    • restricts what can be done with the variable.

    Since the exact type of the class is unknown (only a constraint is known), the compiler has to err on the side of safety and disallow certain operations that are either not co- or contravariant. (If you are not already familiar with it, Co- and contravariance is the scientific background of these types of generics.)

    0 讨论(0)
  • 2020-12-30 17:00

    The topic question is also discussed in several other places like:

    • Generic upper bounded wildcard instantiation known at run time
    • Generics with extends
    • CodeRanch: Generics and use of wildcard
    • CodeRanch: Problem in Generics
    • Oracle: Guidelines for Wildcard Use

    It is actually an question from the book for the Java certificate preparation, from the last test (Question 34). The preparation book is based on the lessons book.

    Even with the explanation here and under the other links and the writing in the book the solution was not clear for me because the explanations are mostly based on the List interface and reading that I thought it is some inner collection specific soluton.

    But if you see the definition of the List interface and the add-method on one side and the the Organic class with its react-method on other side you will notice that they are defined in the similar way.

    public interface List<E> extends Collection<E> {
       ...
       boolean add(E e);
       ...
    }
    
    public class Organic<E> {
        ...
        void react(E e) { }
       ...
    }                                               
    

    So all explanation based on the List interface examples which you can find anywhere are valid for this question too.

    List<? extends String> list1 = new ArrayList<String>();
    List<? super String> list2 = new ArrayList<String>();
    list1.add(new String()); //The method add(capture#1-of ? extends String) in the type List<capture#1-of ? extends String> is not applicable for the arguments (String) - // compile-time error
    list2.add(new Object()); //The method add(capture#2-of ? super String) in the type List<capture#2-of ? super String> is not applicable for the arguments (Object) - // compile-time error
    

    Take a look at the explanation on this one:

    • Oracle: Guidelines for Wildcard Use
    0 讨论(0)
提交回复
热议问题