Executing a method in the parent class automatically after the constructor

Discussion in 'Java' started by 418928@cepsz.unizar.es, Feb 4, 2007.

  1. Guest

    Hi everybody,

    I don't know if there is any trick for this. I have a class A and
    several classes that inherit from A. Each of these classes should call
    always a certain method on its parent A after the constructor has been
    called (i.e., after the class is fully initialized). Is there any way
    to force this automatically? Perhaps using a factory... but I would
    like not to touch the existing code regarding the creation of the
    instances of the subclasses...

    Thanks,
    Sergio
     
    , Feb 4, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    Re: Executing a method in the parent class automatically after theconstructor

    wrote:
    > Hi everybody,
    >
    > I don't know if there is any trick for this. I have a class A and
    > several classes that inherit from A. Each of these classes should call
    > always a certain method on its parent A after the constructor has been
    > called (i.e., after the class is fully initialized). Is there any way
    > to force this automatically? Perhaps using a factory... but I would
    > like not to touch the existing code regarding the creation of the
    > instances of the subclasses...


    I don't think there's a way to do this -- certainly not
    if the subclasses have accessible constructors.

    It seems a peculiar thing to want to do, though. Methods
    in class A are "blind" to the natures of A's subclasses, so
    there's not much an A method can do that makes effective use
    of subclass-specific features; A's method might just as well
    run at the end of A's own constructor. Perhaps you want the
    A method to call a method the subclass is expected to override?
    Well, that seems peculiar, too: It means A is in a sense privy
    to the implementation details of its subclasses, a notion that
    runs counter to the idea of encapsulation ...

    Perhaps if you'd describe the larger problem you're trying
    to solve, someone might suggest a way to solve it without the
    need for "constructor callbacks."

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 4, 2007
    #2
    1. Advertising

  3. Alex Hunsley Guest

    Re: Executing a method in the parent class automatically after theconstructor

    wrote:
    > Hi everybody,
    >
    > I don't know if there is any trick for this. I have a class A and
    > several classes that inherit from A. Each of these classes should call
    > always a certain method on its parent A after the constructor has been
    > called (i.e., after the class is fully initialized). Is there any way
    > to force this automatically?


    No. Why do you want to do this? It sounds to me, perhaps, that you want
    to 'fix' the fact that a subclass isn't compelled to call the super
    class's constructor by enforcing some setup code elsewhere.... (of
    course I may be completely wrong about this!) As Eric asked: what are
    you trying to achieve?

    Anyway, the fact the you're thinking along these lines indicates to me
    that there is something not quite right with the design you have in mind.
    It's quite acceptable when designing a class to specify a contract in
    the documentation along the lines of 'after constructing this class, you
    must call method b() before calling any other methods (e.g. c(), d(), or
    e()), or otherwise an exception/undesirable outcome will occur". This
    way, you're not *forcing* something to happen, but you're also making it
    known (in a reasonable way) what the expected/acceptable interaction
    with your class is.


    A word of warning - in case you're tempted to put extra shenanigans at
    the end of the constructor itself: any constructor that calls a method
    that is non-static, non-final (or non-private) may provide you with some
    odd-seeming surprises later.

    To demonstrate:

    Q: What does the code below output to the screen? (Please don't compile
    and run this - just answer by reading the code.)

    class A {

    public A() {
    setUpSomeThings();
    }

    protected void setUpSomeThings() {
    // some setup code in here
    }

    }

    public class B extends A {
    private int memberVariable = 0;

    public B() {
    super();

    System.out.println("memberVariable = "
    + memberVariable);
    }

    protected void setUpSomeThings() {
    memberVariable = 7;
    }

    public static void main(String[] args) {
    B bInstance = new B();
    }

    }

    Bonus question: What is the output if we change the line:

    private int memberVariable = 0;


    For reasons demonstrated by the code above, constructors should be kept
    as simple as possible. Constructors that have a side effect (such as
    showing a window, or generally performing any action (a verb!) in any
    higher level sense) are to be avoided.

    A constructor generally constructs (sets up) a *thing* (an object - or
    think 'noun') -- and any actions (verbs) that that thing will do should
    usually be triggered by calling a method on that object.

    > Perhaps using a factory... but I would
    > like not to touch the existing code regarding the creation of the
    > ins


    A factory may help - but again, what is the situation you're dealing
    with here?
    lex
     
    Alex Hunsley, Feb 5, 2007
    #3
  4. buggy Guest

    Re: Executing a method in the parent class automatically after theconstructor

    > It sounds to me, perhaps, that you want to 'fix' the fact that a
    > subclass isn't compelled to call the super class's constructor


    Sure you can compel a call to the super's constructor. Don't declare a
    class constructor with an empty parameter.

    ----------------------------
    public abstract class MySuper()
    {
    public MySuper(boolean dummy)
    {
    super();
    // do some special stuff
    }
    }

    class MuSub extends MySuper()
    {
    public MySub()
    {
    }
    }

    ----------------------------
    If you do not call super(true) then a compile time error will result
    "Implicit super constructor MySuper() is undefined"
     
    buggy, Feb 9, 2007
    #4
  5. Daniel Pitts Guest

    On Feb 4, 12:32 pm, "" <>
    wrote:
    > Hi everybody,
    >
    > I don't know if there is any trick for this. I have a class A and
    > several classes that inherit from A. Each of these classes should call
    > always a certain method on its parent A after the constructor has been
    > called (i.e., after the class is fully initialized). Is there any way
    > to force this automatically? Perhaps using a factory... but I would
    > like not to touch the existing code regarding the creation of the
    > instances of the subclasses...
    >
    > Thanks,
    > Sergio


    It sounds like you want to use aspect oriented programming.
    I've never really worked with it, but I've heard of ways to do it.

    Look into AspectJ
     
    Daniel Pitts, Feb 9, 2007
    #5
  6. Mark Rafn Guest

    <> wrote:
    >I don't know if there is any trick for this. I have a class A and
    >several classes that inherit from A. Each of these classes should call
    >always a certain method on its parent A after the constructor has been
    >called (i.e., after the class is fully initialized). Is there any way
    >to force this automatically?


    No, aside from having methods in A that throw IllegalStateException if the
    class isn't properly initialized, and documenting the heck out of it.

    Or you could move the required configuration into A's constructor, which will
    then be executed BEFORE initialization of the subclass. This is the cleanest
    way.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Feb 10, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pantalaimon
    Replies:
    3
    Views:
    51,186
    John Harrison
    Oct 9, 2004
  2. mrstephengross
    Replies:
    5
    Views:
    621
    Larry I Smith
    May 18, 2005
  3. Replies:
    6
    Views:
    521
  4. ali
    Replies:
    4
    Views:
    619
    David Harmon
    Mar 5, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,341
Loading...

Share This Page