So ClassName
has a generic parameter T
and this parameter needs to fit a certain requirement, in this case extends a certain type S
, that means, T
must inherit S
. Now the interesting thing in this case is this S
.
We have S
to be ClassName<?>
, so T
must inherit from ClassName
with a wildcard. For the wildcard aka the question mark please have a look at the link Michael Markidis gave in a comment to your question.
The real fun now is that this definition
public abstract class ClassName<T extends ClassName<?>>
allows recursive generic type defintion. So you could have something like
ClassName<ClassName<ClassName<ClassName<?>>>> test;
for whatever that's worth :)
EDIT: Given
ClassName2<T extends ClassName<?>> extends ClassName<T>
thats relatively easy in comparison. We want to inherit ClassName
but not "destroy" the generic argument, so we take one ClassName
would accept, in this case T extends ClassName<?>
. In extends ClassName<T>
the compiler checks if this (i.e. ClassName2
's) T
fits the T
of ClassName
, which was the requirement (remember ClassName
's definition) T extends ClassName<?>
, so this obviously works.
In addition, we have ClassName2<?>
extending ClassName<?>
, so now you can mix the two types however you want:
ClassName2<ClassName<ClassName<ClassName<?>>>> test2;
ClassName2<ClassName<ClassName2<ClassName<?>>>> test3;
However, if you would have, say
class ClassName3<T extends ClassName3<?>> extends ClassName<T>
(the public and abstrac modifiers don't really influence the generic behavior here), you can only have things like
ClassName3<ClassName3<ClassName3<ClassName3<?>>>> test4;
ClassName2<ClassName<ClassName3<ClassName3<?>>>> test5;
since ClassName
and ClassName2
don't inherit ClassName3
.