mur7ay
mur7ay

Reputation: 833

Creating a subclass to represent a banking account in Java

I'm trying to create a program that will use an Account class and from there create two subclasses, checking and savings that extends the parent Account class. I believe I have this portion of the code completed.

Now here is the two subclasses:

(Checking)

public class Checking extends Account {

    double overdraftLimit = 5000;

    public void withdraw (double w) {

        if (balance - w < overdraftLimit) {
            System.out.println("The exceeds the overdraft limit of $5000");
        } else {
            super.withdraw(w);
        }
    } // End of withdraw method

} // End of Checking.

My problem lies within the final set of instructions. When I go thru my program and attempt to exceed the overdraft limit nothing happens. What could I do to correct this?

Thanks.

Upvotes: 1

Views: 1625

Answers (1)

Maik Fruhner
Maik Fruhner

Reputation: 310

Edit: You are withdrawing from the normal Account, without checking

sa.withdraw(withdrawAmount);

Try no.withdraw(withdrawAmount); But notice, that you create an empty Checking-Account without the input data.

Original post

Your are creating and Object of class Account, which of course does not check your overdraft. To use your new Subclass change

Account sa = new Account(accountNumber, accountBalance, interestRate);

to

Account sa = new Checking(accountNumber, accountBalance, interestRate);

Make sure, that the consturctors exist in your subclass, as it is required in your task!

Java always checks first in the class itself, if the method exists, then in the superclasses. But if you dont even create your subclass, you cant use their methods, of course!

Upvotes: 2

Related Questions