In generics? Super T and? The difference between extends T and superextends

Source: Internet
Author: User

In generics? Super T and? The difference between extends T and superextends

List <? Super T>, Set <? What does extends T> statement mean? <? Super T> indicates the parent class of any T including T. <? Extends T> represents any T subclass, including T. The following describes the differences between the two wildcards.

 

 

Extends

List <? The wildcard declaration of extends Number> foo3 means that the following values are valid:

 

// Number "extends" Number (in this context) List <? Extends Number> foo3 = new ArrayList <? Extends Number> (); // Integer extends Number List <? Extends Number> foo3 = new ArrayList <? Extends Integer> (); // Double extends Number List <? Extends Number> foo3 = new ArrayList <? Extends Double> ();

With the given value assignment statement, what type of elements can be read from the foo3 list? You can read the Number, because the above list either contains the Number element or the Number class element.

You cannot guarantee to read the Integer because foo3 may point to List <Double>.

 

You cannot guarantee to read Double because foo3 may point to List <Integer>.

 

After a given value assignment statement is written, what type of elements can you legally insert to foo3?

You cannot insert an Integer element, because foo3 may point to List <Double>.

 

You cannot insert a Double element, because foo3 may point to List <Integer>.

 

You cannot insert a Number element, because foo3 may point to List <Integer>.

 

You cannot go to List <? Insert any types of objects in extends T>, because you cannot guarantee what type the list actually points to, and you cannot guarantee what types of objects are actually stored in the list. The only guarantee is that you can read the subclass T or T from it.

 

Super

Now consider List <? Super T>.

 

List <? The wildcard declaration of super Integer> foo3 indicates that the following values are valid:

 

// Integer is a "superclass" of Integer (in this context) List <? Super Integer> foo3 = new ArrayList <Integer> (); // Number is a superclass of Integer List <? Super Integer> foo3 = new ArrayList <Number> (); // Object is a superclass of Integer List <? Super Integer> foo3 = new ArrayList <Object> ();

With the given value assignment statement, what type of elements can be read from the foo3 list? You cannot guarantee to read the Integer because foo3 may point to List <Number> or List <Object>.

You cannot guarantee to read the Number because foo3 may point to List <Object>.

 

The only guarantee is that you can read the Object or the Object subclass Object (you do not know what the specific subclass is ).

 

Through the value assignment statement given above, what type of elements can you legally insert into foo3? You can insert an Integer object because the declared list above supports Integer.

You can insert an Integer subclass object because the Integer subclass is also an Integer.

 

You cannot insert a Double object because foo3 may point to ArrayList <Integer>.

 

You cannot insert a Number object because foo3 may point to ArrayList <Integer>.

 

You cannot insert an Object because foo3 may point to ArrayList <Integer>.

 

PECS

Remember the PECS principle: the Producer uses extends and the Consumer uses super.

 

Producer uses extends

If you need a list to provide T-type Elements (that is, you want to read T-type elements from the list), you need to declare the list as <? Extends T>, such as List <? Extends Integer>, so you cannot add any elements to the list.

 

Consumers use super

If you need a list of T-type Elements (that is, you want to add T-type elements to the list), you need to declare the list as <? Super T>, such as List <? Super Integer>, so you cannot guarantee the type of Elements read from it.

 

That is, producer and consumer

If a List is to be produced and consumed, you cannot declare a List using wildcard wildcards, such as List <Integer>.

 

Example

See the copy method (JDK1.7) in java. util. Collections ):

 

 

We can get some inspiration from the code of the Java Development Team. The copy method uses the PECS principle to implement parameter protection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.