Hi all, I'm working on a new assigment which entails "inheritance". I have already written a code for an account class that includes account id, balance annual interest rate and a withdrawl and deposit method. Now I'm suppose to write two subclasses one a savings account and the other a chequing account where the checking account has an overdraft limit and the savings account does not. So write now I'm working on the savings account subclass. Below is the original account class code and then the beginning of my Savings Account subclass. I need some advice desperately. I feel very lost.
As you can tell by my feeble attempt at my subclass I'm very confused . I want the user to be able to input an integer when asked accordingly (amount to withdraw, amount to deposit) and get the proper output. Can anyone start me off on the right track, because I don't think I am. Thanks Stacey
I think the "account" class is your main class. what you can do is to provide a textbox and a button in your class and asking the user to enter the amount to be withdrawn. on the button click (actionperfomed in case of java, you have to use "actionlistener" interface here) you can call your "saving account" class and do the necessary actions. same thing applies to your "chequing account" class.
"Inheritance" is referred to with the phrase "is-a". In your example, you would say "savings account IS A account", and "checking account IS A account" (bad grammer aside). you would not say "savings account IS A TestAccountClass". So, Lalit is correct. I think this is what you were trying to do - or even what you thought you were doing, but just put the wrong class after 'extends'. I think you'll have a problem in your SavingsAccount constructor - i don't see where the variable a is defined (and, to be honest, it's a poor choice for a variable name. it doesn't tell me what it is supposed to be). ok, but back to inheritance. Does your Account class work ok? i didn't look at that code very carefully, but assuming it does... what does a SavingsAccount do differently than an Account? if the answer is nothing, all you have to do is write a constructor that calls the super() constructor, and you're done. by using the word "extends", your sub class does EVERYTHING THE SUPERCLASS DOES (except the constructor thing). you don't even have to mirror ANY calls to ANY method. that's the beauty of inheritance (well, one of them). when you get to your CheckingAccount, it too will derive from account. it may be that all you have to do it override the "withdrawal" method. in the sub class, the method will have the same signiature. then, put in some logic for the overdraft protection. if you pass your tests, call the super.withdraw() method. hope that helps (more importantly, since i've had no coffee yet, hope i didn't say anything wrong!!! good luck!!!
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Jan 11, 2004
Hi, Okay in response to my two responses, one thing I'm quite sure of is I need to write test program to test my savings account subclass, so that is where I would ask for the input and so on. In the course I'm taking so far I haven't been introduced to "actionlistener" so I don't really know what it is or how to use it. I'm trying to keep things as simple as possible. I know I haven't refered to what "a" is yet, and I didn't plan on changing the variable, I just put it there as a reminder because I think I need one but again I'm new at this and I need more help in terms of explanation. ONe of my other questions is, when I write a test account for this class, it will refer to the subclass for the info. It's just a bit weird to me. Also as I said before with my two subclasses that I'm writing I want the user to input to integers one to withdraw with and one to deposit. On the SavingsAccount subclass when the user withdraws too much I a message to say "Insufficent Funds". Thanks for everyones help, Stacey
to ensure that i understand you right. there exists two kinds of accounts saving accounts and chequing account. both have the same attributest (id, balance, annualInterestRate) and the same behavior regarding deposit but a different behavior regarding withdrawl. on an saving account only an amount that ist smaller than balance can be withdrawed, on a chequing account every amount can be withdrawed(in this assignment). then you have to build an abstract superclass, which includes the behaviour, that is the same for both chequing account and saving account with an abstract method withdrawl. then you build two subclasses, SavingsAccount and ChequingAccount which extends account and override the method withdrawl.(fred described something similar in detail) you can to this for example in this way:
Believe it or not, you've almost got the code to test your SavingsAccount. since SavingsAccount derives from Account (or, it will once you change that line), you can change this:
then, everything else will work. side note: you can use System.out.println(), instead of System.out.print(); this will put the newline automatically. your way isn't wrong, but you can save some lines/typing.
Here's a rough framework. You should be able to see how 'most' of the variable/methods are in the Account class. All these are inherited by SavingsAccount and ChequeAccount, these having additional 'unique' variables/methods (did I mention this was rough?)