2

I have a parameterized method that is extended from another class. The abstract class Account is the parent, and SavingsAccount inherits the constructor from it. This constructor is a parameterized constructor. I would like to use a conditional to allow (or disallow) certain values into the double init_balance field which would then invoke the super() for the parent constructor.

if(init_balance < minimumBalance){
    //Print message to user asking for larger deposit. (is this enough to do?)
}

But the java language asks me to place the invocation to the parent constructor first-thing in the child constructor. So I can't filter through the child constructor what goes into the parent constructor.

Here is my code on gist

TrivPants
  • 79
  • 9

4 Answers4

1

I believe this answers this correctly: Why do this() and super() have to be the first statement in a constructor?

But basically you need to put the super(init_id, init_balance); first.

Community
  • 1
  • 1
BetaNyan
  • 27
  • 4
1

As others have said, the language requires that you call the superclass constructor first (I believe this is to avoid possible problems with the subclass constructor accessing superclass fields or methods before the superclass's fields have been initialized). Often that's not a problem except for wasting a few nanoseconds. If it's really a problem--the superclass constructor does something you don't want done if constraints are violated--make the subclass constructor private and use a factory method:

class SavingsAccount extends Account {

    private SavingsAccount(String init_id, double init_balance)
    {
        super(init_id, init_balance);
    }

    public static SavingsAccount newSavingsAccount(String init_id, double init_balance) {
        if (init_balance < minimumSavings) {
            System.out.println("Sorry, but you need to give us moar moneyz!");
            throw new Exception("Not enough money.");
        }
        return new SavingsAccount(init_id, double init_balance);
    }
}

You can no longer say new SavingsAccount(id, balance) in the rest of your code; you have to say SavingsAccount.newSavingsAccount(id, balance). But that price may be worthwhile, depending on what your needs are.

ajb
  • 31,309
  • 3
  • 58
  • 84
1
class Account
{
    public Account(String init_id, double init_balance)
   {
       super(init_id, init_balance);
   }
   public void condition_Method()
   {
      //your condition create object only if condition satisfied or else give                  //error message

      Account object=new Account(init_id, init_balance);
   }
}
Piyush Yawalkar
  • 252
  • 1
  • 4
  • 17
1

If you want to keep a constructor based object creation you can come up with this:

public class SavingsAccount extends Account {

  private SavingsAccount(String init_id, double init_balance)
  {
    super(init_id, validate(init_balance));
  }

  public static double validate(double init_balance) {
    if (init_balance < minimumSavings) {
        System.out.println("Message");
        throw new RuntimeException("Message"); // or handle this error
    }
    return init_balance;
  }
}

Yet - looking at your example I would probably choose to do the validating outside the constructor.

CoronA
  • 7,717
  • 2
  • 26
  • 53