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?
Processing and sending processed data to super from child class constructor
java;design patterns;object oriented design;inheritance;template method
The bad news is that you can't.The good news is that you can.How you cannot:B extends AB is an Aa B instance only can be built upon an existing A instanceYou cannot instantiate B, let alone change it's state, before instantiating AAlso 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.==> 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);    }}