Suppose I need some DerivedBuilder
to extend some BaseBuilder
. Base builder has some method like foo
(which returns BaseBuilder
). Derived builder has method bar
. Method bar
should be invoked after method foo
. In order to do it I can override foo
method in DerivedBuilder
like this:
@Override
public DerivedBuilder foo() {
super.foo();
return this;
}
The problem is that BaseBuilder
has a lot of methods like foo
and I have to override each one of them. I don't want to do that so I tried to use generics:
public class BaseBuilder<T extends BaseBuilder> {
...
public T foo() {
...
return (T)this;
}
}
public class DerivedBuilder<T extends DerivedBuilder> extends BaseBuilder<T> {
public T bar() {
...
return (T)this;
}
}
But the problem is that I still can not write
new DerivedBuilder<DerivedBuilder>()
.foo()
.bar()
Even though T
here is DerivedBuilder
. What can I do in order to not to override a lot of functions?
new DerivedBuilder<DerivedBuilder>().foo().bar()
. It will work and executefoo
first and thenbar
. If you want to call more methods ofBaseBuilder
and lastly you want to callDerivedBuilder
method, then it's not possible, because second time, method returns reference ofBaseBuilder
, with this you can't callDerivedBuilder
's method. – Mccabebar
was not defined inBaseBuilder
because then I do.foo().bar()
the only thing compiler knows aboutT
after executingfoo
is that thisT
extendsBaseBuilder
– Macadam