1

I want to do some initialization in child class constructor and pass result to super().

But Java doesn't allow any processing in child class constructor before super() call.

Whats a good way to solve this problem?

3
  • Possible duplicate of Choosing the right Design Pattern
    – gnat
    Aug 4 '16 at 8:56
  • I come here to find answer ... please dont downvote before reading question properly ... I did not find solution in given post
    – mzlo
    Aug 4 '16 at 8:59
  • 1
    the "good design pattern" is to avoid doing what you want as explained eg here: "The object initialization sequence is complex enough as it is and already sometimes causes headaches. Allowing subclasses to run code before the superclass constructor would make it more complex and confusing still, with more potential for subtle bugs, especially if there is a hierarchy of multiple classes which all do this..."
    – gnat
    Aug 4 '16 at 9:05
0

The bad news is that you can't.

The good news is that you can.

How you cannot:

  • B extends A
  • B is an A
  • a B instance only can be built upon an existing A instance
  • You cannot instantiate B, let alone change it's state, before instantiating A
  • Also remember than, after calling super(), any change you make to the state of the superclass is done to the subclass, unless you are hiding members, i.e., declaring a member or field in the subclass with the same name as one of the superclass when the superclass member is visible to the subclass.

How you can:

To solve that situation, don't use inheritance, use composition. But take in mind that both A and B shouldn't depend on each other. Both should depend on an abstraction (for example an interface) that both (or maybe just A) implement.

Strictly speaking this is not composition but aggregation but, in a loose way of speaking, we are using composition instead of inheritance.

enter image description here

==> I.java <==

public interface I {
    public void setChild(I i);
    public void setParent(I i); 
    public void setX(String s);
    public void setY(int n);
    public void setZ(boolean b);
}

==> A.java <==

/* This class is abstract only for Eclipse to allow me 
   to leave out all the method stubs
   for the sake of conciseness of the example 
   (I like my examples to compile) */

public abstract class A implements I {

    private I child;

    @Override
    public void setChild(I i) {
        this.child= i;
        this.child.setParent(this);
    }

}

==> B.java <==

/* This class is abstract only for Eclipse to allow me 
   to leave out all the method stubs
   for the sake of conciseness of the example 
   (I like my examples to compile) */

public abstract class B implements I {

    private I parent;

    @Override
    public void setParent(I i) {
        this.parent = i;
        // now modify the state of the "parent"
        this.parent.setX("Answer to the Ultimate Question of Life, The Universe, and Everything");
        this.parent.setY(42);
        this.parent.setZ(true);
    }

}
0

I would favour composition over inheritance, and do something like:

public class Super(Child child) {
}

(naming for consistency with the question above - not good otherwise) and inject the class dependency in. Relying on construction order in an inheritance hierarchy is difficult, problematic and doesn't allow a subsequent decomposition/disentanglement of the dependent classes.

As soon as I find myself in such a situation, I take a step back and ask what I'm really trying to achieve. The result (most usually) looks like the above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.