Java wildcard extends and super

Source: Internet
Author: User
Keyword description

●? Wildcard type

● <? Extends T> indicates the upper bound of the type, indicating that the parameterized type may be a subclass of T or T.

● <? Super T> indicates the lower boundary of the type (supertype limitation in Java core). It indicates that the parameterized type is the supertype of this type (parent type) until the object

Extends example
 
Static class food {}
Static Class fruit extends food {}
Static class Apple extends fruit {}
Static class redapple extends Apple {}

List <? Extends fruit> flist = new arraylist <Apple> ();
// Complie error:
// Flist. Add (new Apple ());
// Flist. Add (new fruit ());
// Flist. Add (new object ());
Flist. Add (null); // only work for null

List <? Extends frut> indicates "a list with any types inherited from fruit". The compiler cannot determine the type of the list, so it cannot safely add objects to it. Null can be added, because null can represent any type. Therefore, the add method of list cannot add any meaningful elements, but it can accept the existing sub-type list <Apple> value assignment.

Fruit fruit = flist. Get (0 );

Apple = (Apple) flist. Get (0 );

Because the placement is a type inherited from fruit, you can safely retrieve the fruit type.

Flist. Contains (new fruit ());

Flist. Contains (new Apple ());

When the contains method in collection is used, the object parameter type is accepted. Wildcards are not involved, and the compiler allows this call.

Super example

List <? Super fruit> flist = new arraylist <fruit> ();

Flist. Add (new fruit ());

Flist. Add (new Apple ());

Flist. Add (New redapple ());

// Compile error:

List <? Super fruit> flist = new arraylist <Apple> ();

List <? Super fruit> indicates "a list with any fruit super type". The list type must be at least one fruit type. Therefore, you can safely Add the fruit and its child types to it. Because list <? The type in super fruit> may be any fruit super type, and cannot be assigned to the list of fruit sub-types of Apple <Apple>.

// Compile error:

Fruit item = flist. Get (0 );

Because list <? The type in super fruit> may be any fruit super type, so the compiler cannot determine whether the object type returned by get is fruit, or the parent class of fruit, food or object.

Summary

Extends can be used to limit the return type, but cannot be used to limit the parameter type.

Super can be used for parameter type limitation and cannot be used for return type limitation.

Wildcard characters with super-type restrictions can be easily written to generic objects. Wildcards with extends subtypes can be read from generic objects. -- Core Java

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.