12
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?

rvillablanca
  • 1,606
  • 3
  • 21
  • 34

3 Answers3

9

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.

Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
0

The only difference I can notice is that method can be of T class or any inherited classes.

Edorka
  • 1,781
  • 12
  • 24
0

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.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186