3

Does java support class Foo<T super X> ,if no,why ?

Where T is a type parameter and X is a type.

optional
  • 3,260
  • 5
  • 26
  • 47

3 Answers3

6

No. The super keyword in the context of Generics could only be used in combination with a wildcard (?) when declaring (consumer) variables or method parameters (consumers) that are of some Generic type.

For example, these are valid:

List<? super Something> list = someListReference;

public void methodThatPopulatesAList(List<? super Something> consumer) {
    ...
    list.add(new Something());
    ...
}

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

you can use generic declaration Foo<? super X> in order to make things like this possible:

List<? super Integer> foo3 = new ArrayList<Object>();

but you can't use it in class declaration

Cootri
  • 3,806
  • 20
  • 30
0

For Class :

class Foo<T extends X>

For Object :

Object<? extends X>
Dev
  • 115
  • 1
  • 9