Requiring multiple upper bounds extends A B

suggest change

You can require a generic type to extend multiple upper bounds.

Example: we want to sort a list of numbers but Number doesn’t implement Comparable.

public <T extends Number & Comparable<T>> void sortNumbers( List<T> n ) {
  Collections.sort( n );
}

In this example T must extend Number and implement Comparable<T> which should fit all “normal” built-in number implementations like Integer or BigDecimal but doesn’t fit the more exotic ones like Striped64.

Since multiple inheritance is not allowed, you can use at most one class as a bound and it must be the first listed. For example, <T extends Comparable<T> & Number> is not allowed because Comparable is an interface, and not a class.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Generics:
* Requiring multiple upper bounds extends A B

Table Of Contents
6 Generics
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap