The difference between super and extends in Java paradigm

Source: Internet
Author: User

In fact, a word is:

Super is best used as an output parameter, extends is best used as input parameter;


Take a look at the following example:

Private Class A {//} private class B extends A {//} private class C extends B {//        } public void Test () {list<? super b> test = new ArrayList ();        Test = new arraylist<a> ();        Test = new arraylist<b> (); Test = new arraylist<c> (); Cannot accept a Class A = Test.get (0) smaller than B;//cannot get b b = test.get (0);//Cannot get c c = test.get (0);//Cannot get TES        T.add (New A ());//But cannot add a class Test.add (new B ()) larger than B;        Test.add (New C ()); list<?        Extends b> test1 = new ArrayList ();        Test1 = new arraylist<a> ()//cannot accept class test1 = new arraylist<b> () larger than B;        Test1 = new arraylist<c> ();        A a1 = test1.get (0);        B B1 = test1.get (0); C C1 = test1.get (0);//But cannot get a class Test1.add (new A ()) smaller than B,//Cannot add Test1.add (new B ());//Cannot add test1.add (NE W C ());//Cannot add}

The following is a compilation error in Eclipse:


Specific reasons can be found in:

Http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java

Http://docs.oracle.com/javase/tutorial/java/generics/capture.html

The difference between super and extends in Java paradigm

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.