Alex Miel wrote:I have two classes that inherit abstract class and implement interface. In abstract class I have variables common for both implementation. I also need to construct classes using same builder, which is generic and static in abstract class. Unfortunately it's impossible in Java to call constructor for generic parameter. I want to avoid making specific builder for all implementation. Is there some way to do this?
Yes but, like the others, I suspect it's not what you want.
1. Builders are usually used to create
complex objects - especially ones that have many "default" values - not generic ones.
2. If the subtypes have
identical contents, why are they subtypes? You may have a reason, but I think you need to explain it.
What
is possible is that your
Bar class constructor takes a
Builder<? extends Bar> and uses it to initialize
its fields, while the subclass has something like a
make(Builder<SubClass>) method (defined as
abstract in
Bar) that uses a "dummy" constant of its own type. Your generic
Builder could then call:
return T.make(this);
in its
build() method. But it's quite tortuous, and I suspect there's a more elegant solution.
Winston