0

Is this possible to call super constructor on some condition? I have below code

private static final class AuditDataTable extends AuditDataModel<DiscrepanciesVO> {

        private AuditDataTable (){
            super();       
        }
}

But i will want to call super() when some Boolean class variable is true .How can i achieve this ?

Now above super() will call this

 public AuditDataModel() {
        super();
        AuditResulstBean.setAuditResultRowCount(0);
        AuditResulstBean.setAuditResultRowCheck(false);
    }

But i want top level class super() constructor not called when some condition is false.

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • 3
    not. whether you add the super call yourself or not, it is/has to be the first thing the constructor does. – Stultuske Jan 22 '16 at 11:22
  • The `super()` call always has to be the first call in a constructor. You cannot avoid it. – LordAnomander Jan 22 '16 at 11:22
  • 1
    You could call `super(boolean)` (and create the respective constructor in your superclass) though and do whatever you want to do if it is `true`/`false`. – LordAnomander Jan 22 '16 at 11:24
  • But that is fishy. your constructor can not possibly know whether that boolean is passed by a construcotr of a subclass or some other way. – Stultuske Jan 22 '16 at 11:26
  • 1
    You have to call the constructor of a parent class. This is not optional. – Peter Lawrey Jan 22 '16 at 11:30
  • If it is just a boolean, it would be easier to provide two constructors (one with just a ghost parameter to differ them). Then you dont need to change the super constructor – ctst Jan 22 '16 at 11:37
  • Yes it would be a Boolean variable . – Subodh Joshi Jan 22 '16 at 11:38
  • This is very much a design smell. Either the superclass should know about that boolean (in which case you can just pass it to a `super(boolean)` constructor, or not. If it doesn't need to know about the boolean, its initialisation must be independent of whatever you do in the subclass. – biziclop Jan 22 '16 at 11:49
  • 1
    It would be interesting to see the real problem to be solved and understand where this slightly smelly design comes from. If the super class needs to be initialized, then must be initialized. If the subclass does not need the initialization, maybe it does not need the parent's functionality, it which case it should not be a subclass. – Harald Jan 22 '16 at 11:54

2 Answers2

0

This is not nice, but it works (not a direct answer to your question, but a solution to your problem). You have to choose before, which constructor you init, hence you won't need dependencies on your super. Beware that doSuper is never used and the point of decision is backwarded. As requested a nearly full working example:

private static final class AuditDataTable extends AuditDataModel<DiscrepanciesVO> {

    private AuditDataTable(boolean doSuper){
        super();
    }

    private AuditDataTable(){
        //other constructor
    }

    AuditDataTable getAuditDataTable(boolean useSuper){
        AuditDataTable auditDataTable;
        if(useSuper)
            auditDataTable = new AuditDataTable(true);
        else
            auditDataTable = new AuditDataTable();
        return auditDataTable;
    }
}
ctst
  • 1,610
  • 1
  • 13
  • 28
  • 1
    It's his constructor used. He can call it from within (thats the reason, why my added constructor is also private)... pls think before voting down! – ctst Jan 22 '16 at 11:29
  • The old answer didn't work, since `this` is final... my apologies. – ctst Jan 22 '16 at 11:41
  • @subodh You make the choice of decision when you initialise. An example would be: `AuditDataTable auditDataTable; if(yourBoolean) auditDataTable = new AuditDataTable(true); else auditDataTable = new AuditDataTable();` – ctst Jan 22 '16 at 12:34
  • @SubodhJoshi with the method getAuditDataTable(boolean) you get an example, where you have to apply your new decision. – ctst Jan 22 '16 at 12:44
  • @ctst Rather than calling constructor directly i have to call `cgetAuditDataTable()` – Subodh Joshi Jan 22 '16 at 12:49
  • In the way I wrote it down it would be a way (but then you might prefer this method to be public and static), but you can also directly copy the method-content. This depends on your personal taste and how you want to use it. – ctst Jan 22 '16 at 12:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101407/discussion-between-ctst-and-subodh-joshi). – ctst Jan 22 '16 at 13:03
0

In any cases, the constructor calls the super constructor.

The question to choose call it / dont call it is bad asked.

See that:

public class UpperClass {
public UpperClass()
{   
System.out.println("UpperClass");
}
public UpperClass(int a)
{
System.out.println("UpperClass 1");
}
}

And

public class LowerClass extends UpperClass {
    public LowerClass()
    {
    super(1);
    System.out.println("LowerClass");
    }
}

Without or with super(1), when creating an LowerClass object, you get:

UpperClass

LowerClass

or

UpperClass 1

LowerClass

Then the question is: which one super constructor you want , with which criteria ?

Then

1 you must have 2 (or more) super constructors, or only one with arguments

2 you have to pass this criteria from the subclass to the superclass