63

Why can I use super only with wildcards and not with type parameters?

For example, in the Collection interface, why is the toArray method not written like this

interface Collection<T>{
    <S super T> S[] toArray(S[] a);
}
Paul Bellora
  • 51,514
  • 17
  • 127
  • 176
mohsenof
  • 633
  • 1
  • 6
  • 5

5 Answers5

51

super to bound a named type parameter (e.g. <S super T>) as opposed to a wildcard (e.g. <? super T>) is ILLEGAL simply because even if it's allowed, it wouldn't do what you'd hoped it would do, because since Object is the ultimate super of all reference types, and everything is an Object, in effect there is no bound.

In your specific example, since any array of reference type is an Object[] (by Java array covariance), it can therefore be used as an argument to <S super T> S[] toArray(S[] a) (if such bound is legal) at compile-time, and it wouldn't prevent ArrayStoreException at run-time.

What you're trying to propose is that given:

List<Integer> integerList;

and given this hypothetical super bound on toArray:

<S super T> S[] toArray(S[] a) // hypothetical! currently illegal in Java

the compiler should only allow the following to compile:

integerList.toArray(new Integer[0]) // works fine!
integerList.toArray(new Number[0])  // works fine!
integerList.toArray(new Object[0])  // works fine!

and no other array type arguments (since Integer only has those 3 types as super). That is, you're trying to prevent this from compiling:

integerList.toArray(new String[0])  // trying to prevent this from compiling

because, by your argument, String is not a super of Integer. However, Object is a super of Integer, and a String[] is an Object[], so the compiler still would let the above compile, even if hypothetically you can do <S super T>!

So the following would still compile (just as the way they are right now), and ArrayStoreException at run-time could not be prevented by any compile-time checking using generic type bounds:

integerList.toArray(new String[0])  // compiles fine!
// throws ArrayStoreException at run-time

Generics and arrays don't mix, and this is one of the many places where it shows.


A non-array example

Again, let's say that you have this generic method declaration:

<T super Integer> void add(T number) // hypothetical! currently illegal in Java

And you have these variable declarations:

Integer anInteger
Number aNumber
Object anObject
String aString

Your intention with <T super Integer> (if it's legal) is that it should allow add(anInteger), and add(aNumber), and of course add(anObject), but NOT add(aString). Well, String is an Object, so add(aString) would still compile anyway.


See also

Related questions

On generics typing rules:

On using super and extends:

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    This way Collection" super Integer> should take every thing. Because every thing is Object and Object is super type of Integer. – mohsenof May 10 '10 at 05:18
  • @mohsenof: even though `String` is an `Object`, a `Collection` is not a `Collection super Integer>`, because generics are invariant: a `Collection` is NOT a `Collection`. A `String[]` however is an `Object[]`, because arrays are covariant. Again, generics and arrays don't mix, and they run under very different type rules. Read _Effective Java 2nd Edition, Item 25: Prefer lists to arrays_. – polygenelubricants May 10 '10 at 05:23
  • 6
    so why would List addToList(List list, T element){ list.add(element); return list; } not make sense? – ILMTitan May 10 '10 at 14:27
  • 22
    "it wouldn't do what you'd hoped" - this is just plain wrong. The OP provided a great use case (modulo the dreaded array covariance) with obvious semantics: the collection should be able to fill the array of any more general type and return the said array. There is nothing wrong with it. – Rotsor Apr 15 '11 at 01:55
  • 3
    "since any array of reference type is an `Object[]`, it can therefore be used as an argument to ` S[] toArray(S[] a)`" True. However, if `S` were inferred to be `Object`, then that would also constrain the return type of the method to `Object[]`, which may cause a compile error depending on the context in which the call is made (if it expects a `S[]`). So changing the type variable to `Object` is not a substitute for a `super` bound. – user102008 Oct 31 '12 at 23:29
  • 4
    there are some, though few, valid use cases for lower bounds. – ZhongYu Mar 15 '13 at 16:14
  • 8
    polygenelubricants: The main point in this answer, that upper type bound are not useful, is incorrect. This is demonstrated in Rotsor's answer. I would like to encourage you to edit this answer to point this out, because in its current form it is misleading people! – Lii Dec 10 '14 at 17:15
  • @polygenelubricants, ` super T>` is **ILLEGAL**!! Are you sure!! – KGhatak Apr 22 '17 at 20:41
  • 3
    -1 This is answer is incorrect. The real answer is because "They designed Java that way". See Rotsor's answer for valid (and extremely useful) use cases that are invalid code only because of the Java specifications, not because the logic is incorrect. – Ben Seidel Sep 27 '18 at 06:36
  • 1
    Yes, the answer is wrong. It sounds convincing at first but it's nonsense. `T super String` means `T` is either `Object`, `CharSequence` or `String` - but not anything else. Clearly that is a real - and useful - bound. – Stefan Reich Jun 27 '20 at 17:26
  • What if the hypothetical clause `` is allowed, and if we were to use it along with a classy alternative such as `Class`. Then we would be able to solve this problem? So the new version of `toArray` would be: ` S[] toArray(Class a)` – Ravindra Ranwala Dec 22 '20 at 05:25
39

As no one has provided a satisfactory answer, the correct answer seems to be "for no good reason".

polygenelubricants provided a good overview of bad things happening with the java array covariance, which is a terrible feature by itself. Consider the following code fragment:

String[] strings = new String[1];
Object[] objects = strings;
objects[0] = 0;

This obviously wrong code compiles without resorting to any "super" construct, so array covariance should not be used as an argument.

Now, here I have a perfectly valid example of code requiring super in the named type parameter:

class Nullable<A> {
    private A value;
    // Does not compile!!
    public <B super A> B withDefault(B defaultValue) {
        return value == null ? defaultValue : value;
    }
}

Potentially supporting some nice usage:

Nullable<Integer> intOrNull = ...;
Integer i = intOrNull.withDefault(8);
Number n = intOrNull.withDefault(3.5);
Object o = intOrNull.withDefault("What's so bad about a String here?");

The latter code fragment does not compile if I remove the B altogether, so B is indeed needed.

Note that the feature I'm trying to implement is easily obtained if I invert the order of type parameter declarations, thus changing the super constraint to extends. However, this is only possible if I rewrite the method as a static one:

// This one actually works and I use it.
public static <B, A extends B> B withDefault(Nullable<A> nullable, B defaultValue) { ... }

The point is that this Java language restriction is indeed restricting some otherwise possible useful features and may require ugly workarounds. I wonder what would happen if we needed withDefault to be virtual.

Now, to correlate with what polygenelubricants said, we use B here not to restrict the type of object passed as defaultValue (see the String used in the example), but rather to restrict the caller expectations about the object we return. As a simple rule, you use extends with the types you demand and super with the types you provide.

Rotsor
  • 13,009
  • 5
  • 40
  • 57
17

The "official" answer to your question can be found in a Sun/Oracle bug report.

BT2:EVALUATION

See

http://lampwww.epfl.ch/~odersky/ftp/local-ti.ps

particularly section 3 and the last paragraph on page 9. Admitting type variables on both sides of subtype constraints can result in a set of type equations with no single best solution; consequently, type inference cannot be done using any of the existing standard algorithms. That is why type variables have only "extends" bounds.

Wildcards, on the other hand, do not have to be inferred, so there is no need for this constraint.

@###.### 2004-05-25

Yes; the key point is that wildcards, even when captured, are only used as inputs of the inference process; nothing with (only) a lower bound needs to be inferred as a result.

@###.### 2004-05-26

I see the problem. But I do not see how it is different from the problems we have with lower bounds on wildcards during inference, e.g.:

List<? super Number> s;
boolean b;
...
s = b ? s : s;

Currently, we infer List<X> where X extends Object as the type of the conditional expression, meaning that the assignment is illegal.

@###.### 2004-05-26

Sadly, the conversation ends there. The paper to which the (now dead) link used to point is Inferred Type Instantiation for GJ. From glancing at the last page, it boils down to: If lower bounds are admitted, type inference may yield multiple solutions, none of which is principal.

Ben Schulz
  • 5,581
  • 1
  • 16
  • 14
  • 3
    Underappreciated answer, should be accepted. Lower bounds in method type parameters are useful (as Rotsor showed) and can be implemented (as the author of that paper showed when he did so in Scala). Them being _hard_ to implement sensibly is clearly the real reason they aren't allowed in Java. – Malnormalulo Aug 03 '17 at 16:46
-1

Suppose we have:

  • basic classes A > B > C and D

    class A{
        void methodA(){}
    };
    class B extends  A{
        void methodB(){}
    }
    
    class C extends  B{
        void methodC(){}
    }
    
    class D {
        void methodD(){}
    }
    
  • job wrapper classes

    interface Job<T> {
        void exec(T t);
    }
    
    class JobOnA implements Job<A>{
        @Override
        public void exec(A a) {
            a.methodA();
        }
    }
    class JobOnB implements Job<B>{
        @Override
        public void exec(B b) {
            b.methodB();
        }
    }
    
    class JobOnC implements Job<C>{
        @Override
        public void exec(C c) {
            c.methodC();
        }
    }
    
    class JobOnD implements Job<D>{
        @Override
        public void exec(D d) {
            d.methodD();
        }
    }
    
  • and one manager class with 4 different approaches to execute job on object

    class Manager<T>{
        final T t;
        Manager(T t){
            this.t=t;
        }
        public void execute1(Job<T> job){
            job.exec(t);
        }
    
        public <U> void execute2(Job<U> job){
            U u= (U) t;  //not safe
            job.exec(u);
        }
    
        public <U extends T> void execute3(Job<U> job){
            U u= (U) t; //not safe
            job.exec(u);
        }
    
        //desired feature, not compiled for now
        public <U super T> void execute4(Job<U> job){
            U u= (U) t; //safe
            job.exec(u);
        }
    }
    
  • with usage

    void usage(){
        B b = new B();
        Manager<B> managerB = new Manager<>(b);
    
        //TOO STRICT
        managerB.execute1(new JobOnA());
        managerB.execute1(new JobOnB()); //compiled
        managerB.execute1(new JobOnC());
        managerB.execute1(new JobOnD());
    
        //TOO MUCH FREEDOM
        managerB.execute2(new JobOnA()); //compiled
        managerB.execute2(new JobOnB()); //compiled
        managerB.execute2(new JobOnC()); //compiled !!
        managerB.execute2(new JobOnD()); //compiled !!
    
        //NOT ADEQUATE RESTRICTIONS     
        managerB.execute3(new JobOnA());
        managerB.execute3(new JobOnB()); //compiled
        managerB.execute3(new JobOnC()); //compiled !!
        managerB.execute3(new JobOnD());
    
        //SHOULD BE
        managerB.execute4(new JobOnA());  //compiled
        managerB.execute4(new JobOnB());  //compiled
        managerB.execute4(new JobOnC());
        managerB.execute4(new JobOnD());
    }
    

Any suggestions how to implement execute4 now ?

==========edited =======

    public void execute4(Job<? super  T> job){
        job.exec( t);
    }

Thanks to all :)

========== edited ==========

    private <U> void execute2(Job<U> job){
        U u= (U) t;  //now it's safe
        job.exec(u);
    }
    public void execute4(Job<? super  T> job){
        execute2(job);
    }

much better, any code with U inside execute2

super type U becomes named !

interesting discussion :)

nxdrvr
  • 67
  • 4
-1

I really like the accepted answer, but I would like to put a slightly different perspective on it.

super is supported in a typed parameter only to allow contravariance capabilities. When it comes to covariance and contravariance it's important to understand that Java only supports use-site variance. Unlike Kotlin or Scala, which allow declaration-site variance. Kotlin documentation explains it very well here. Or if you're more into Scala, here's one for you.

It basically means that in Java, you can not limit the way you're gonna use your class when you declare it in terms of PECS. The class can both consume and produce, and some of its methods can do it at the same time, like toArray([]), by the way.

Now, the reason extends is allowed in classes and methods declarations is because it's more about polymorphism than it is about variance. And polymorphism is an intrinsic part of Java and OOP in general: If a method can accept some supertype, a subtype can always safely be passed to it. And if a method, at declaration site as it's "contract", should return some supertype, it's totally fine if it returns a subtype instead in its implementations

yuranos
  • 5,211
  • 4
  • 42
  • 50
  • The accepted answer is wrong. It's correct that this question is about type bounds and not about variance. But lower type bounds using `T super R` would sometimes be useful, as Rotsors' answer demonstrates, and also the reference to the Guava [`Optional`](https://github.com/google/guava/blob/b72102d427be43b5c092042c9a9e5d972d55510c/guava/src/com/google/common/base/Optional.java#L187) class. – Lii Oct 13 '19 at 18:32