r/javahelp • • Aug 04 '25
Wildcards question
For example:
class Parent<T> {}And now we make a subclass:
class Child<T extends Number> extends Parent<T>Are these two statements equivalent?
Child<?> child1
Child<? extends Number> child2Obviously, java automatically converts <?> to <? extends Object> but is this case when there is a bounded type parameter does it convert it to ? extends Bound (in this case <? extends Number>)?
3
Upvotes
View all comments
Show parent comment
1
u/severoon pro barista Aug 05 '25
The trick to dealing with complex generics is simply to read
?as "something."Your example above
Child<? extends Number>would be read, "Childof something that extendsNumber."The other thing when writing complex generics is that if you are dealing with more than one or two straightforward generic types, Use more comprehensive type names suffixed with
T. For instance, if you are working with a type that has three generic types, a number, a vector, and a graph node:Foo<NumberT, VectorT, GraphNodeT>is going to be much easier to work with thanFoo<N, V, G>.