> Rethink the class hierarchy. What you want isn't I->A->B; you want
>
[quoted text clipped - 8 lines]
> abstract), and can't use super.foo (since it's abstract) or A.foo
> (which is not applicable to "this").
> I'm not sure if you are aware that the following solution is also
> possible (although I suspect that Twisted's or Zig's solution might be
> the better one for your problem)
I've now seen Zig's posting, and he eventually arrived where I did by
a circuitous route, aside from his A being an anonymous implementation
returned by an ABase factory method instead of a standalone public
final class. (My names for the classes but describing his structure.)
> class A implements I {
> public void foo() {}
[quoted text clipped - 10 lines]
>
> }
Nothing here is stopping someone directly extending A. And A can't be
made final or B_Base can't exist.
A could be given only package-private constructors and B_Base (in the
same package) a protected or public one. Then it more-or-less works,
unless someone's willing to define their class into someone else's pre-
existing package. :)
However, my version gives what I feel to be a cleaner type hierarchy
-- logically, there is A-that-can-be-extended-but-requires-foo-
implementing and A-that-is-a-directly-usable-implementation.
Separating those into abstract ABase and final A tidies up and keeps
what should be subclassed determined by "final" rather than
constructor hijinks.
And of course anyone who mentions that A's constructor could have code
like "if (!(getClass().equals(A.class)) && !(this instanceof B_Base))
throw..." will be flamed to a crispy br<claps hands over mouth and
turns red>
Ingo R. Homann - 26 Jun 2007 10:07 GMT
Hi Twisted!
>>class A implements I {
>> public void foo() {}
[quoted text clipped - 13 lines]
> Nothing here is stopping someone directly extending A. And A can't be
> made final or B_Base can't exist.
Well, of course, Mize-ze has to provide B_Base, so it *will* exist. The
other point (directly extend A) is true but can be solved as you showed
(package-private constructor). Anyhow...
> However, my version gives what I feel to be a cleaner type hierarchy
> -- logically, there is A-that-can-be-extended-but-requires-foo-
> implementing and A-that-is-a-directly-usable-implementation.
> Separating those into abstract ABase and final A tidies up and keeps
> what should be subclassed determined by "final" rather than
> constructor hijinks.
...I totally agree to this. (That was what I meant with "Twisted's ...
solution might be the better one".)
> And of course anyone who mentions that A's constructor could have code
> like "if (!(getClass().equals(A.class)) && !(this instanceof B_Base))
> throw..."...
:-)
Ciao,
Ingo