Search code examples
javainheritancesubclasssuperclassbank

Calling a superclass method from a subclass


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);
            }
    }

Solution

  • 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);
            }
    }