Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

generics

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

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

like image 992
optional Avatar asked Dec 07 '25 08:12

optional


1 Answers

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:

  • What is PECS?
  • Wildcards in Java
like image 156
Konstantin Yovkov Avatar answered Dec 09 '25 21:12

Konstantin Yovkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!