When should a subclass have its own fields and when should it use its super

When should a subclass have its own fields and when should it use its superclass' fields?
Hi, thank you for reading this post!
Let me use a specific example to ask my question.
public class BankAccount {
     private double accountBalance;
     public double getBalance() {
          return this.accountBalance;
public class SavingsAccount extends BankAccount {
     private double accountBalance;
     public double getBalance() {
          return this.accountBalance;
}In the bank account example, both BankAccount and SavingsAccount will have a method getBalance(). Therefore, they
both require a account balance field. My question is since getBalance() for both classes will perform the exact same
operation, when should I omit declaring the getBalance() method and the accountBalance field in the subclass, and
when should I include them?
My own thought is when we never have to instantiate a superclass object (e.g. an abstract class), then we place
common fields in the abstract superclass and have subclasses access these fields via protected getter/setters to
access the superclass' fields. This is the principle of reuse.
But when you do need to instantiate a superclass and the superclass does need to maintain its own fields, then
I would need to duplicate the accountBalance field and getBalance() method in the subclass.
Is my thinking correct or incorrect?
Thank you in advance for your help!
Eric
Edited by: er**** on 22-Aug-2011 20:19

er**** wrote:
If SavingsAccount inherit BankAccount.getBalance()...getBalance() would return BankAccount's accountBalance. This is NOT the correct result we want.Actually, I think it's precisely what you want.
We want getBalance() to return BankAccount's accountBalance when we use a BankAccount object, and SavingsAccount's accountBalance when we use a SavingsAccount object.I seriously doubt that. I think you're confusing a BankAccount with a Customer, who can have more than one account.
In every system I've ever seen, a SavingsAccount IS-A BankAccount - that is to say, it's a genuine subtype. Now, it may well contain other fields ('interest'?) that a normal account wouldn't, but 'balance' ain't one of them.
Winston

Similar Messages

Maybe you are looking for

  • Adapter needed to connect to external monitor

    I'm trying to connect my macbook to an external monitor. I ordered a VGA Display Adapter but it doesn't fit in the mini-dvi port on my notebook. So I ordered a mini-dvi to VGA adapter, but the pictures I've seen of this adapter do not look like what

  • What happened I just asked a question, registered, got the email, activated, got to the plugin diagnosis and then got an error report

    Can you please resend me the link as something went wrong when I tried to update the plugins.

  • Using Word templates

    Hi, I want to export my Robohelp 7 HTML help to a Word document. I want the output to look exactly like the main help file, but want to add some headers and footers using a Word template, Here's what I've tried: In the Single Source Layouts> Printed

  • Print out of sales tax pre-announcement

    One of our customers demands some changes within the Business One. The support in Ireland advised us to post these requirements in this Forum. At the moment it's not possible to get a print out of the sales tax pre-announcement. In our customers opin

  • Activation Problems Continue

    Day 5 and I still have a pretty dead desktop ornament. The following phone numbers may be of help to the many thousands like me. It takes about two hours to get into the 60 minute que at the Iphone Activation Support line clearly indicating the probl