On the extends and Super keywords in Java generics

Source: Internet
Author: User

Generics are added in Java 1.5, and the details of generics are not discussed here, which is very clear in the fourth edition of Thinking in Java, which is about the super and extends keywords, and why there are different limitations when using these two keywords.
First, we define two classes, A and B, and assume that b inherits from a. The following code, which defines several static generic methods, is not particularly well-written, and we mainly consider the problem of compilation failure:

 Public classgeneric{//Method One Public Static<textendsA>voidGet (list<textendsA>list) {List.get (0);}//Method Two Public Static<textendsA>voidSet (list<textendsA>list, a a) {List.add (a);}//Method Three Public Static<tSuperB>voidGet (list<tSuperB>list) {List.get (0);}//method Four Public Static<tSuperB>voidSet (list<tSuperB>list, b b) {List.add (b);}}

After compiling, we will find that method two and method three have no way to compile. According to thinking in Java, Super represents the nether, and extends represents the upper bound, method two is not the way to pass, because it is placed in the list may be a, or any a subclass, so the compiler has no way to ensure type safety. And method three failed to compile, because the compiler does not know whether get out is B or the other sub-class B, because set method four allows to put in the list B, also allows to put a subclass of B in the list, there is no way to guarantee type safety.
The above explanation may sound a bit strange, because the compiler cannot determine whether a and B itself or the other subclasses of a and b are causing the failure. So why not Java simply use a keyword to fix it?
If explained from the following angle, it is possible to explain the question of why the compilation error is more straightforward and clear, but also easier to understand, first look at the following code, or a and B two classes, B inherits from a:

 Public classgeneric2{ Public Static voidMain (string[] args) {List<?extendsA> List1 =NewArraylist<a>(); List<?extendsA> List2 =NewArraylist<b>(); List<?SuperB> List3 =NewArraylist<b>(); List<?SuperB> List4 =NewArraylist<a>(); }}

It is easier to understand the meaning of the super and extends keywords from the above section of the code that created the list. One of the first things to illustrate is that Java enforces the need to make specific types of type parameters when creating objects, and cannot use wildcards, meaning new arraylist< Extends A> (), New arraylist<?> () This form of initialization statement is not allowed.
From the first and second lines of the main function above, we can understand the meaning of extends, when creating ArrayList, we can specify a or B as the specific type, that is, if < extends X> So when we create an instance, we can use X or the class that extends from x as the generic parameter as the specific type, or it can be understood as a given. Specifies a specific type, which is what extends means.
Similarly, the third row and the fourth line are explained, if <? Super X> When we create an instance, we can specify any superclass of x or X as the concrete type of the generic parameter.
When we use LIST<? Extends x> This form, invoking the list's Add method will cause compilation to fail, because when we create a specific instance, it may be that X is used as a subclass of X, and this information compiler has no way of knowing, and for arraylist<t For >, only one type of object can be placed. This is the nature of the problem. For the Get method, because we create an instance by using a subclass of X or X, and using a superclass to refer to subclasses in Java, we can get a reference to an X type by the Get method, which can point to X or to any subclass of X.
And when we use LIST<? When super x> this form, the Get method that calls list will fail. Because when we create an instance, we may use an X or a superclass of x, so when we call get, the compiler is not exactly aware of it. Instead of calling the Add method, because we created an instance using the superclass of x or X, it is certainly not a problem to add a subclass of X or X to the list, because a superclass reference can point to a subclass.
Finally, the two keywords appear because generics in Java do not have the inverse of covariant properties.

http://mysun.iteye.com/blog/851925

On the extends and Super keywords in Java generics (GO)

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.