Java generic wildcard character extends, super

Source: Internet
Author: User

Citation: http://sharewind.iteye.com/blog/1622164

Keyword description
    • ? Wild-Letter wildcard type
    • <? Extends t> represents the upper bound of the type, which indicates that the parameterized type may be a subclass of T or T
    • <? Super t> represents the lower bound of the type (called the superclass in Java core), which indicates that the parameterized type is a supertype of this type (the parent type) until the object
extends example

list<? Extends frut> means "with any list of inherited types from fruit", the compiler cannot determine the type that the list holds, so it is not safe to add objects to it. You can add null, because null can represent any type. So the Add method of the list cannot add any meaningful elements, but it can accept the existing subtype list<apple> assignment.

Fruit fruit = flist.get(0);Apple apple = (Apple)flist.get(0);

Because, where placement is a type inherited from fruit, the fruit type can be safely removed.

flist.contains(new Fruit());flist.contains(new Apple());

When using the Contains method in collection, the object parameter type is accepted, and no wildcard characters are involved, and the compiler allows this to be called.

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> represents "a list with any Fruit superclass", and the list type is at least one Fruit type, so you can safely add Fruit and its subtypes to it. Due to list<? The type in super fruit> may be of any Fruit type and cannot be assigned a subtype of Fruit Apple list<apple>.

// compile error:Fruit item = flist.get(0);

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

Summary
extends 可用于的返回类型限定,不能用于参数类型限定。super 可用于参数类型限定,不能用于返回类型限定。>带有super超类型限定的通配符可以向泛型对易用写入,带有extends子类型限定的通配符可以向泛型对象读取。

My understanding:

Generalization: Specifies an object type for the container. In Java, subclasses can be assigned to the parent class container , that is, the parent class cannot be assigned to the child class container , because the downward assignment needs to add information such as a new property, which cannot be done, that is, cannot be assigned downward.

= = = "list<?" Super Fruit >

    • The list contains the parent class of Fruit and Fruit, but it is not clear what the parent class is at compile time.
    • When you assign a value directly to a List, you must satisfy the container object requirements , and you must assign a value of Fruit or Fruit parent class. Because compile-time, fully meet the requirements, so can be compiled. At run time, you must determine what type of object is placed in the List. If you put Fruit subclasses, then you do not know at all what type of Fruit subclasses to generalize, so the final run-time List of object type is indeterminate.
    • you can add any Fruit and Fruit subclasses to the List . Because at compile time, it can be determined that the list is at least Fruit, so the Fruit and Fruit subclasses can at least assign a value up to Fruit, of course, it may also be assigned to the Fruit parent class, so the compilation passes. At run time, the list is stored in the specific Fruit which parent class has been determined, the add element will be converted directly to the parent class, so run through.
    • super is used for the add element, not for the Get element . Because the compile-time is not sure which parent class, because of the generalization qualification, get can only be assigned to the current type and its parent class, then can only be assigned to object objects. So Object o = flist.get (0) is possible.

= = = "list<?" Extends Fruit >

    • It means that the List contains Fruit and Fruit subclasses , but it is not clear which subclass to compile.
    • When you assign a value directly to a List, you must satisfy the container object requirements , which must be assigned a subclass of Fruit or Fruit. It is not possible to assign a parent class here, because it violates the basic requirements of generalization.
    • The Get element can be assigned to the Fruit and Fruit parent classes . Because the list contains at most the Fruit class, the Fruit and Fruit parent classes are satisfied when the value is assigned upward.
    • the extends is used for the get element, not the add element . You cannot add elements to the List. Because the compile time is not sure which Fruit subclass is stored in the list, if add a subclass, eg. Orange, and the actual storage is Apple, then the underlying type does not match. So you can only add null.

Java generic wildcard character extends, super

Related Article

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.