Invokation of overridable methods from a constructor

Hi!

I know this is a classic issue, but I 'd really need some advice on this topic.

Considering possible harmful consequences to this practice, I would like to know if there is a way to use the Template pattern within a constructor.

In my context, I think it would be reasonable to have an abstract class with a constructor to call abstract methods that would be defined in subclasses.

Is there any way to put the Template pattern in practice with a constructor?

Thanks in advance,

Tales

[531 byte] By [talesToninia] at [2007-11-26 22:25:39]
# 1
As far as I can see, the only problem with calling abstract methods is that fields of subclasses are not initialized before the call to the abstract constructor returns. I.e., any changes done in the overriden abstract methods to fields will most probably be void when returning from
stefan.schulza at 2007-7-10 11:26:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Why do you have to use the constructor as a template method?If you do no have to, why do you want to use the constructor as a template method?What business requirement indicates such an awkward design?
GhostRadioTwoa at 2007-7-10 11:26:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
It should never be done. No dancing around it. Sometimes I let it go if its package scope only. But there is really no need and its bad practice. I made a PMD rule for this long ago.What you should do instead is use a static factory method.
_dnoyeBa at 2007-7-10 11:26:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
I thank you all very much for helping me undestand the incompatibility inherent to that idea. Special thanks for _dnoyeB for the alternate suggestion.See ya!
talesToninia at 2007-7-10 11:26:39 > top of Java-index,Other Topics,Patterns & OO Design...