The difference between extends and super in Java generics

Source: Internet
Author: User

The general distinction between extends and super in generics is described in this way:

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

Look at this I am not quite understand, change to vernacular is this meaning:

list<? Extends t> is to say that this list is a subtype of T or T object, but cannot determine what type, so get (), Cannot add () (can add null value)

list<? Super T> is saying that this list has at least the object of type T, so I can add t or T sub-type, but get the type is not OK, so can not get

Cite an example of the internet everywhere

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子类型限定的通配符可以向泛型对象读取。

 

Here, the pecs principle is as follows: generic reading? Extends T (t{t type or subtype of T} type is not deterministic, the compiler does not know the specific type when writing, so it will report a compiler Error) {last}, write? Super T (t{t parent type up to object}, read object) {Next}, generics are for type-safe

If you want to read data of type T from the collection and cannot write, can I use the? extends wildcard character; (Producer extends) if you want to write data of type T from the collection, and you do not need to read it, you can use it? Super wildcard; (Consumer super) do not use any wildcard characters if you need to save and take them again.

The difference between extends and super in Java generics

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.