Suppose I have a class
public static class A<T extends D> { ... }
and the class D
with two classes extending it: B
and C
, e.g.
public static class D { ... }
public static class B extends D { ... }
public static class C extends D { ... }
Now, at some place let's say I want an array of A
's, irrespective of being of the B
-kind or the C
-kind. (And apply functions from class D
to all items in the array, for example.)
Should I then constrain the type again?
In other words: which of these options is the one to go with?
A<?>[] re;
A<? extends D>[] re;
Which one is best practice?
Since
T
has an upper bound ofD
,A<?>
is just a shorthand forA<? extends D>
. They both mean the same thing - just like ifT
were unbounded,A<?>
would be short forA<? extends Object>
.I don't know of any best practice when it comes to this syntax; I think it's just a matter of coding style. I would prefer
A<?>
because it's concise, thoughA<? extends D>
immediately communicates the upper bound to a developer unfamiliar withA
.