Overriding Methods

Posted 06 July 2011 - 09:43 PM

I'm having some trouble trying to finish this homework assignment. I'm supposed to ask the user for an account type, saving accounts can not be overdrawn and checkings accounts have a -500 overdraft limit. I already have the mainframe of the code, I'm having trouble overriding the withdraw method in the account class.

I'm a bit confused about the validation of the input, would I need to do all the validation in the account class? If so would I need to pass the user input (amount to withdraw) to the class so I can do the validation?
Also wouldnt I need to change the withdraw method from void to something else so I can get the validation back?

Re: Overriding Methods

Posted 07 July 2011 - 04:06 PM

Update
I added some more functions/code that are part of the requirements of the assignment.
I added a second account, one for savings and the second one for checkings.
I added a deposit option for both account.
I also added toString() for each class.
The problems I'm having now is implementing the overriding and how to use/display the toString

Re: Overriding Methods

Posted 07 July 2011 - 08:11 PM

Why is balance a static variable? All the Account objects share that variable. It doesn't make sense. Static variables are associated with the class, not the objects. It should be an instance variable.

Re: Overriding Methods

Posted 07 July 2011 - 08:34 PM

macosxnerd101, on 08 July 2011 - 04:11 PM, said:

Why is balance a static variable?

On top of that, there are a few other design issues with this code, but its functional... its probably a good idea to get it working as per the required spec, then look at the glaring problems with the way its written and improve its OO-yness. If you're interested in improving your code and programming skills that is.

As an exercise, try commenting out your over-ridden toString method, and then try the System.out.println line from above. Now make a couple of accounts and print them both. What do you notice?

Cheers

Steve

WOW!!! That literally just blew my mind, I now got a better understanding of how the overriding works!
But wait, does that mean I need to do something along those lines for the withdraw method?

Its awesome when the lightbulbs come on

You already override the withdraw method, although not particularly elegantly. To explain a little: You're using something called inheritance when you create the Account class, then the savings and cheque 'subclasses'. These subclasses 'inherit' (hence the name) all the methods from the superclass (Account) - and this means any method that Account has can be used with any savings or a cheque object that you might create. You can override the ones that need to be specifically modified for a particular type of account (ie: the $500 OD for the cheque account).

On a more global scale, toString() is a method of the Object class, which is the granddaddy of all classes in Java - so all classes you create inherit it automatically. You generally override it to make it useful for the class (if its required).

Re: Overriding Methods

Posted 07 July 2011 - 09:19 PM

shango11, on 07 July 2011 - 05:07 PM, said:

I'm a bit confused about the validation of the input, would I need to do all the validation in the account class? If so would I need to pass the user input (amount to withdraw) to the class so I can do the validation?
Also wouldnt I need to change the withdraw method from void to something else so I can get the validation back?

Yes the validation should be done in CheckingAccount and SavingAccount
If your main driver does the validation... no need to have 2 separate classes
And Yes you can modify withdraw() to be a boolean to return true or false depending if the transaction was accepted or refused