简体   繁体   中英

subclass and superclass in Java

over here i've tried to make a subclass BasicAccount from the superclass BankAccount. Whilst making a withdrawal method that will not withdraw more money than there currently is in the account.

But i still don't understand why i can't access it in BasicAccount even though the variable is private in BankAccount. Any idea on how to access the balance in my withdrawal method, by still keeping the balance field private?

/**
 A bank account has a balance that can be changed by
 deposits and withdrawals.
 */
class BankAccount
{
    private double balance;

    /**
     Constructs a bank account with a zero balance.
     */
    public BankAccount()
    {
        balance = 0;
    }

    /**
     Constructs a bank account with a given balance.
     @param initialBalance the initial balance
     */
    public BankAccount(double initialBalance)
    {
        balance = initialBalance;
    }

    /**
     Deposits money into the bank account.
     @param amount the amount to deposit
     */
    public void deposit(double amount)
    {
        double newBalance = balance + amount;
        balance = newBalance;
    }

    /**
     Withdraws money from the bank account.
     @param amount the amount to withdraw
     */
    public void withdraw(double amount)
    {
        double newBalance = balance - amount;
        balance = newBalance;
    }

    /**
     Gets the current balance of the bank account.
     @return the current balance
     */
    public double getBalance()
    {
        return balance;
    }
}
    class BasicAccount extends BankAccount{


    BasicAccount(double initialBalance) {

    }


        public void withdraw(double amount) {
            if (amount > 0 && this.balance - amount >= 0) {
                super.getBalance() -= amount;
            } else if (amount < 0) {
                throw new IllegalArgumentException("Withdraw amount should be positive and greater than 0.");
            } else {
                System.out.println("Error: Withdraw amount exceeds available funds.");
            }
        }



}

class Main {
    public static void main(String args[]) {
        BankAccount account = new BasicAccount(100.00);
        double balance = account.getBalance(); //expected 100.00;

        account.withdraw(80.00);
        balance = account.getBalance(); //expected 20.00;

        account.withdraw(50.00);
        balance = account.getBalance(); //expected 20.00 because the amount to withdraw is larger than the balance
    }
}

You need to call super.withdraw(double) from the subclass.

Also

super.getBalance() -= amount;

This does nothing. You cannot subtract and assign the value to a method, only to a variable. This isn't logical. Replace with what i said, super.withdraw(amount).

Also

in BasicAccount#withdraw you have this.balance but you mean to say super.balance because this class, BasicAccount, doesn't define a balance class member, however the super class BankAccount does.

Also

        BasicAccount(double initialBalance) {

        }

You need to call super(initialBalance) because right now you're not giving calling the super constructor which assigns balance.

        BasicAccount(double initialBalance) {
            super(initialBalance);
        }

Also (oof)

        public BankAccount() {
            balance = 0;
        }

Instead of creating a default constructor that does the same thing your other one does, either delete it since balance is 0 by default, or call the other constructor that has purpose.

        public BankAccount() {
            this(0);
        }

Now your initial balance constructor can do some useful edge case checks that the no-args constructor benefits from.

    public BankAccount(double initialBalance) {
        if (initialBalance < 0) {
            throw new IllegalArgumentException("Initial balance cannot be below zero.");
        }
        balance = initialBalance;
    }

If you want to access a field of a superclass from a subclass you have to declare it protected instead of private. You will then be able to access it with super.variableName .

Also see what @Jason wrote, he pointed out other mistakes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM