19

Is it possible to specify both upper and lower bound constraints on type parameters in Java?

I found a conversation in Sun's forum in which this issue was discussed (apparently before the generics feature was finalized), but there was no final answer.

In summary, is there a valid syntax to do the following?

public class MyClass<T extends Number super Integer>

3 Answers 3

14

I don't believe so - as far as I can tell from the language specification, "super" is only valid for wildcard types in the first place. The syntax for wildcards also suggests you can only have one wildcard bound, too - so you can't use something like this either:

// Invalid
void foo(List<? extends Foo super Bar> list)

Even though both of these are okay:

// Valid
void foo(List<? extends Foo> list)

// Valid
void foo(List<? super Bar> list)

As noted in comments, it's possible to have multiple upper bounds - but only for type parameters and cast expressions. For example:

// Valid
<T extends Number & Comparable> void foo(List<T> list)
5
  • 1
    Thanks for your answer. May I suggest that you add a comment saying that the code is not valid, so that nobody thinks (falsely) that it's valid? (e.g. if it appears in a search engine results page)
    – Hosam Aly
    Mar 27, 2010 at 19:32
  • To be clear, you can have multiple upper bounds, eg List<? extends Number & Comparable>.
    – NateS
    Jul 20, 2017 at 1:09
  • @NateS: Yup - I've added that.
    – Jon Skeet
    Jul 20, 2017 at 5:43
  • 1
    List<? extends Number & Comparable> actually still doesn't compile. (example) You can see the grammar still doesn't allow for it either: 4.5.1. The bound can only be a reference type. In contrast, type variables and cast expressions do allow for the additional bound.
    – Radiodef
    Aug 10, 2017 at 17:42
  • 1
    @Radiodef: Thanks, will fix.
    – Jon Skeet
    Aug 10, 2017 at 17:52
6

From Oracle's tutorial:

Note: You can specify an upper bound for a wildcard, or you can specify a lower bound, but you cannot specify both.

1

you can't specify both at the same time but you can achieve like given code.

class Family<F> {
    F f;

    public void setF(F f) {
        this.f = f;
    }
}

class GrandParent {
}

class Parent extends GrandParent {
}

class Child extends Parent {
}

private <T extends Parent> void foo(Family<? super T> list) {
    list = new Family<Parent>(); // Allows
    list = new Family<GrandParent>(); // Allows
    list = new Family<Child>(); // Not Allows

    list.setF(new GrandParent()); // Not Allows
    list.setF(new Parent()); // Not Allows
    list.setF(new Child()); // Not Allows
}

public void bar() {
    foo(new Family<GrandParent>()); // Allows
    foo(new Family<Parent>()); // Allows
    foo(new Family<Child>()); // Allows
}
1
  • But this is not what was desired, is it? In foo the type argument can stand for anything that is a super-type of T - including types which do not extend Parent, right?
    – Tom
    Feb 28, 2020 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.