Java paradigm of T-extends comparable<? Super t>

Source: Internet
Author: User
Tags comparable

In the observation of Java source code, the discovery of such a notation T extends comparable< Super t>. Can't help wondering why it's written like this? What are the benefits, extends and super's role here is a bit unclear.

Next, I'll share the code with you about my view of generics here.

1.<t extends comparable< what does super t>> mean ?

    • As you can see, this applies to generics in Java, so let's start by explaining the role of extends here.

Extends followed by type, such as < any character extends class/interface > represents the upper bound of a generic type. The sample code is as follows:

    • After understanding the upper limit of the generics represented by extends, let's look at the role of super, which, contrary to extends, represents the lower bound of the generic type.
    • So combined with the above two points, let's analyze what the whole meaning of this sentence means. First, extends limits the generic limit that T must be comparable< Super t> subclasses, then <? Super t> means that the lower limit of type in comparable<> is T!

2. <t extends comparable<t>> and <t extends Comparable<? What's the difference between Super t>>

The next step is to make a deeper impression of why you want to write code like this.

    • <t extends Comparable<t>>

It means that the type T must implement the Comparable interface, and that the type of the interface is T. In this way, instances of T can compare size to each other. Here we take the class GregorianCalendar in Java as an example.

The code looks like this:

Compile the error here, because here the <t extends comparable<t>> equivalent <gregoriancalendar extends Comparable<gregoriancalendar >>, but GregorianCalendar did not realize Comparable<gregoriancalendar>, but only held comparable< inherited from the calendar. Calendar>, this will cause an error if it is not within the limits.

    • <t extends Comparable<? Super t>>

It means that the type T must implement the Comparable interface, and that the type of the interface is t or the any parent class of T. After this declaration, the instances of T and the instances of the parent class of T can compare size to each other. Also, take GregorianCalendar as an example. The code looks like this:

At this point the compilation passes, this can be understood as <gregoriancalendar extends comparable<calendar>>! Because the Calendar is the parent class of GregorianCalendar and GregorianCalendar implements Comparable<calendar>, it can be viewed in the API!

3. Example code Demo

The code looks like this:

The above code consists of three classes:

1. Animal implements the Comparable<animal> interface to compare the size of instances by age

2. Dog inherits from Animal and is its subclass.

3. The test class provides two sorting methods and a main () method for testing:

    • MySort1 () using <t extends comparable<t>> type parameter
    • MySort2 () use <t extends Comparable<? Super t>> type parameter
    • Main () test method. Here you will create a sequence of animal and dog two respectively, and then test it by calling the sort method.

3.1 Test for MySort1 (), the main method code is as follows:

Result compilation error, error message is:

The type parameter of the MySort1 () method is <t extends Comparable<t>>, which requires the type parameter to be comparable of type T.

If the incoming list<animal> program will execute normally, because Animal implements the interface comparable<animal>.

However, if the parameter passed in is the List<dog> program will error, because the dog class does not implement interface Comparable<dog> it inherits only one comparable<animal> interface from Animal.

Note: The animals list actually contains an instance of a dog. If you encounter a similar situation (the subclass list cannot be passed into a method), consider putting the class instance into a parent list to avoid compilation errors.

3.2 Test for mySort12 (), the main method code is as follows:

At this time we found that the program can run normally. It can not only accept Animal implements comparable<animal> such parameters, but also receive: Dog implements comparable<animal> such parameters.

3.3 Is it possible to solve the problem by implementing the comparable<dog> of the Dog?

The problem with a program that can be analyzed is that the Dog class does not implement interface Comparable<dog>, so can we implement interface comparable<dog> for this class to solve the problem?

The code looks like this:

The resulting program compiles an error message as follows:

The meaning is that the dog class has inherited the comparable interface from animal and can no longer implement a comparable.

If a subclass needs to use its own comparison method, it needs to override the public int CompareTo (Animal) method of the parent class.

4. Summary

For Animal/dog the two classes have a parent-child relationship: <t extends comparable< Super T>> can accept List<animal>, and can also receive list<dog>. And <t extends comparable<t>> can only receive list<animal> so, <t extends comparable<? A type parameter such as Super t>> has fewer restrictions on the parameters passed in and increases the flexibility of the API. In general, the least restrictive type parameters are used in order to ensure type safety.

Java paradigm of T-extends comparable<? Super t>

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.