Java generic boundary problem, super keyword

Source: Internet
Author: User

Background
    • Java given a specific type parameter a after the generic list, with the given another specific type parameter x of the generic list
    • Super or extends can define a large class of generics as the parent class for generics that give specific type parameters.
    • Super or extends defines a bounded generic that overrides the hierarchical relationship between the decision and the specific parameter type generics, based on the hierarchy of the parameter types.
Not to super words too literally.

The Super keyword, which is used in a method of a class to represent a reference to the parent class object.
The generic nether is indicated in the generic boundary syntax.

Assuming an inheritance relationship, a <-B <-C <-D <-E

   void f( List<? super C>  param){...}

List<? super C>Yes,, List<B> List<A> List<C> List<Object> or List<? super B> , List<? super A> the parent class, for a method that is accepted as a parameter, List<? super C> f can implicitly be converted to security when passing the above types. In a method, the item being fetched is of type object, and List<? super C> the type being removed is object, not C, or any other specific class .

If you need to cast out the item for a specific type, the programmer himself guarantees that the existence of objects in the list can be safely forced to transform. The best scenario is that you don't need to deal with a specific type of item, followed by the programmer's own assurance that all item can be safely forced to transform.

Method is unacceptable f List<D> or List<E> , cast forced transformation cannot be compiled, Java does not accept two types of forced transformations without hierarchical relationships. This is a generic rule: they and List<B> , without a List<A> List<C> parent-child relationship.

In the Assumption method

     List<? super C>  param;     param.add(new B());// ERROR     param.add(new Object());//ERROR     param.add(new D());//right

List<? super C>The limit to the item is very large and looks a little strange: any C-parent and C-type objects cannot be stored, and C's subclasses can be deposited. Seems to contradict the parent-child relationship stipulated in the previous section List<...> .

Firstof all, the super-restricted generics are not suitable for storage, they are only suitable for consumption .

    PECS(producer extends,Consumer super)规则。
    • Why LIST<? Super c> Param take out an object, not a specific type?

Because in the declaration param only ask this param is list< Super C> The adult word is a statement param is List<B> , List<A> , List<C> , List<Object> or is List<? super B> , List<? super A> one of the types that cannot be guaranteed to be specific, so the compiler does not know which type of item is being taken out. If the programmer is able to guarantee that an item of the same type is passed in, it will be forced to transform itself, but the compiler can simply contract List<? super C> the type of object that was taken from it.

    • Why can't I deposit List<? super C> a B object type, but can deposit C and D E?

Because depositing a B or object type may break the type consistency of the original list, it is in order to avoid this problem to introduce a generic attribute in JDK5 and therefore cannot be allowed. When depositing a subclass of C, the implicit conversion to C ensures that the item type in the list is not destroyed.

    • Since there is no guarantee that the item taken is the same specific type, what is the use of super?

Super does not limit f the death of what kind of specific list< At the same time, it also makes a certain limit, cannot be,,, List<B> List<A> List<C> List<Object> or List<? super B> , List<? super A> outside of the list. If you pass a forced transformation without boundaries, List<?> that's another thing.

Scenario (for some reason, the subclass De's item list does not need to be processed by F):

    void f( List<? super C>  param){        if(param.get(0) instanceof A){            for(Object item : param){                A a = (A)item;                a.doAsA();            }        } else(param.get(0) instanceof B){             for(Object item : param){                B b = (B)item;                b.doAsB();            }        }    }    

The above scene code is a bit far-fetched, did not think of the actual application scenario. This may also be the keyword super limit generic nether This Java feature is not enough, but it is not a bad thing to provide a choice outside of extends.

Extends

The Pecs rule indicates when extends is used to produce item to generics.

    List<? extends C>  void f(){        List<? extends C> ret = getNewList();        for(int i =0; i<10; i++){            ret.add(new C());            ret.add(new D());        }        return ret;    }

fThe calling code can get a list, and the compiler ensures that the item in the list is of type C. Better to understand, it seems extends is more powerful, is there a nicer scenario using super than using extends to qualify generic boundaries?

It is probably when the upper bound does not need to limit the time, then if the use of < Extends Object>, then it is equivalent to No limit.
So super is not completely can be replaced by extends, one more choice is better.

Java generic boundary problem, super keyword

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.