Let's have class A
with a method useful for chaining:
class A {
A foo() {
// do stuff
return this;
}
}
You could now do a.foo().foo().foo()
(think: builder pattern). Let class B extend it:
class B extends A {
}
B b;
Now invoking b.foo()
returns actual type B
, but declared A
. Therefore I cannot write:
B other = b.foo();
I would have to write either:
B other = (B) b.foo();
Or override foo()
in B:
class B extends A {
@Override
B foo() {
super.foo();
return this;
}
}
Is there a nice way to do this?