In your case, the two approaches are effectively equivalent. They both restrict the argument's type to MyObject<...>
or a subtype.
Since your example methods return void
there's no real benefit from making the method generic. The only important thing for your method is that the argument is a MyObject<...>
—beyond that the real type is meaningless. Adding the ability to make the argument's type more specific adds nothing for the method's implementation and does nothing for the caller. In other words, it's irrelevant "fluff".
So for your examples, I would say prefer the non-generic option. It's cleaner and more straightforward.
However, if your methods returned the given argument back to the caller then making the method generic could prove useful; it would allow you to declare the return type as T
. This opens up possibilities to the caller such as method chaining or invoking the method "inside" another method call, all based on the specific type passed as an argument. An example of this in the core library would be Objects.requireNonNull(T)
.
Another good case for making the method generic is mentioned by @Thilo in the comments:
Another case would be if your method takes multiple arguments. Then you can introduce a T
to make sure those two arguments have the same type (instead of two distinct types that happen to [fulfill] the constraints individually).
void
, prefer the latter. If they returned the argument then using the former could be useful since you could returnT
(an example would beObjects.requireNonNull
). – OxyacidT
if you want to use it in more than one place (return type or multiple method arguments or generic argument with multiple types itself, such asPair<T,T>
) – Triage