0

I'm modelling a bank account with a superclass Account and a subclass SavingsAccount that can't be overdrawn. The makeWithdrawal() method, when called from the main class, should check if the withdrawal is greater than the balance and prompt for input, then edit the balance.

How can I call the makeWithdrawal() method from Account and override it in SavingsAccount using the super keyword? My compiler is giving me "error: incompatible types: missing return value.

Method in Account:

double makeWithdrawal(double withdrawal)    {
    return balance -= withdrawal;
}

(Pretty simple.) This method was initially abstract, but it was causing errors.

Method in SavingsAccount:

    public double makeWithdrawal(double withdrawal) {
        double tempbalance = getBalance();
        if (withdrawal > getBalance())  {
            withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
            return;
        }
        else    {
            return super.makeWithdrawal(withdrawal);
            }
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

4 Answers4

1

The issue is not with your call to super.makeWithdrawl(), which is correct. It is with the empty return statement in the if clause above it. Your logic to get the update the withdrawal amount is sensible, but you need to return the new balance when you do that. I would recommend just moving the call to super.makeWithdrawl() outside the conditonal:

if(withdrawal > getBalance())  {
    withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
}
return super.makeWithdrawal(withdrawal);

This will use the updated withdrawal amount if the original one was too large.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

in the if

if (withdrawal > getBalance())  {
            withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
            return;
        }

you need to return double value rather than simple returning.

Isha Agarwal
  • 420
  • 4
  • 12
0

The problem is with

return;

You should replace it by

return withdrawal;

    public double makeWithdrawal(double withdrawal) {
    double tempbalance = getBalance();
    if (withdrawal > getBalance())  {
        withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
        return withdrawal;
    }
    else    {
        return super.makeWithdrawal(withdrawal);
        }
}
-1

If I understood you correctly:

Account is the base class and SavingsAccount the subclass?

then: super.makeWithdrawal(withdrawal);

also make sure you inherit when defining the class:

public class SavingsAccount extends Account{...}
0x45
  • 779
  • 3
  • 7
  • 26