Probably right.

In the example below, some may argue that the javadoc (here aimed at
sub-classers) on the parent class' constructor could say (as loudly as
possible, but probably never quite loudly enough) that doStuff() is called
before all state is fully initialised (in this case, eye is still null)  ie
sub-classers over-riding doStuff() must be aware that getEye() will return
null during initialisation (construction) if called in the over-riding
doStuff() implementation. However, in java, sub-classers can't be forced to
pay any attention to this.

I agree that this variant of the "template method pattern" (for java
constructors) is at best dangerous: sub-classers are almost invited to try
to do stuff that their state may not yet be initialised for; 
eg: if the over-ridden method (which is the implementation that will
actually be run by the super-class' constructor - as in polymorphism) tries
to use inherited state.

Here's an example for anyone who is still wondering what all the fuss is
about: 

public class Parent{
    private Eye eye; 
    public Parent{
        doStuff();
        eye = new Eye("black");
    }
    protected void doStuff(){  // consider marking this final
        // do some stuff 
    }
    final protected void getEye(){return eye;}
}

class Child extends Parent{
    // for illustration; explicitly declared (and explicitly calls super() ) 
    public Child(){
        super(); // will throw an NPE (in doStuff() ); eye is not
initialised yet
    }
    public void doStuff(){
        super.doStuff(); // nice try, but doesn't help here
        getEye().getColour(); // getEye() will return null when called by
Parent's constructor
    }
}

Regards - Cemal
http://jWeekend.co.uk jWeekend.co.uk 
  


igor.vaynberg wrote:
> 
> On 11/3/07, Al Maw <[EMAIL PROTECTED]> wrote:
>> There's nothing to stop you making your constructor call methods to
>> initialise things that people can then override.
> 
> erm. really? while there is nothing stopping you, you clearly shouldnt...
> 
> -igor
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Jira-issue-moved-to-the-list%3A-constructors-and-init-of-components-tf4743674.html#a13569154
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to