interface Base<T extends Base> {
T method();
}
Against this pattern design
interface Base {
Base method();
}
The only, I guess, with method()
in Base
I can get the specific type.
Are there more benefits?
interface Base<T extends Base> {
T method();
}
Against this pattern design
interface Base {
Base method();
}
The only, I guess, with method()
in Base
I can get the specific type.
Are there more benefits?
You just save one cast. Here is an example:
class A implements Base<A> {
...
}
A a = ...;
A b = a.method();
vs
class A implements Base {
...
}
A a = ...;
A b = (A)a.method();
You can also build on it using T parameter all over the place. Consider accepting T as a parameter or defining a local variable of type T, for example.
The only difference I can notice is that method can be of T class or any inherited classes.
It is actually very clear. In the second case one can return any subclass
of Base2
. In the first case there is no such ambiguity.