Differences between super T and extends T in Java generics

Source: Internet
Author: User

Differences between super T and extends T in Java generics

The declaration of List <super T> and Set <extends T> is often found. What does it mean? <Super T> represents the parent class of any T including T, <extends T> represents any child class of T including T, next we will analyze the differences between the two wildcards in detail.

Extends

 
 
  1. The wildcard declaration of List <extends Number> foo3 means that the following values are valid:
  2.  
  3. // Number "extends" Number (in this context)
  4.  
  5. List <extends Number> foo3 = new ArrayList <extends Number> ();
  6.  
  7. // Integer extends Number
  8.  
  9. List <extends Number> foo3 = new ArrayList <extends Integer> ();
  10.  
  11. // Double extends Number
  12.  
  13. 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 insert any types of objects to the List <extends T>, because you cannot guarantee what type the List actually points to and what type 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>.

 
 
  1. The wildcard declaration of List <super Integer> foo3 means that the following values are valid:
  2.  
  3. // Integer is a "superclass" of Integer (in this context)
  4.  
  5. List <super Integer> foo3 = new ArrayList <Integer> ();
  6.  
  7. // Number is a superclass of Integer
  8.  
  9. List <super Integer> foo3 = new ArrayList <Number> ();
  10.  
  11. // Object is a superclass of Integer
  12.  
  13. 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 Object subclass objects without knowing 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: Producer) uses extends and 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 this list as <extends T>, for example, List <extends Integer>. Therefore, 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>, for example, you cannot guarantee the type of the Elements read from List <super Integer>.

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.