EDIT: I changed a bit the example for getting the idea:
Like
<Integer or Float>
…without having to create a common interface and make a subclass for Integer and Float to implement it
If not, something like this would maybe have more sense and be useful
<E extends Number> <E = (Integer|Float)>
If ? is a wildcard why should not we allowed to restrict certain types?
In very extreme cases (pre-Java 7 without
AutoCloseable), I would have liked to be able to do that, too. E.g.That would’ve allowed me to call
E.close(), no matter what the actual type was. In other words,Ewould contain the “API intersection” of all supplied types. In this case it would containclose(), and all methods fromjava.sql.Wrapperandjava.lang.Object.But unfortunately, you cannot do that. Instead, use method overloading, e.g.
Or plain old
instanceofOr fix your design, as you probably shouldn’t have to intersect APIs of arbitrary types anyway