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
-
How do I remove an unwanted toolbar? I added a radio station to my ibook and it set up its own toolbar so when I go to Safari I have to use it. It involves using Bing for everything as well. It's very annoying and I want to remove it. I can't just type in a URL and have it go to the site. It takes me to all of Bing's choices. Also, when it anticipates what I'm typing (incorrectly) and I have to delete it's guesses all the time. Can anyone help?
Have you looked at Safari's menu bar > view> toolbars to see if it can be unchecked from there,also see if there is a button to collapse the toolbar on the bar itself.
-
i have a iphone4s and an ipod 4th gen, they both share same itunes acc, the ipod has its own email but when people try to FT it also rings on my phone, how do i stop it ringing both devices
That's because you're using the same Apple ID on both devices. On your phone: Settings>FaceTime>You Can Be Reached at>Remove your Apple ID.
-
I have bought an ipad, and when i go to reset it, it needs the restrictions password which i dont know. When i plug it into itunes, it asks to turn find my iphone off, which needs the password of the old owners itunes.What should i do?
If you're able to contact the person who set the lock, send them this link and tell them to follow its instructions.
If not, you can't use the device but may be able to get it refunded. Apple won't remove the lock for anyone other than the original owner(assuming they're alive.)
(119278) -
hey i have this problem with my mac.. that its on its own shuts itself whenever i m using google chrome..i updated to yosemite but the problem never ended and i m experiecing unexplained shutdowns
Hi doctor.saddam,
If your MacBook Pro is shutting down unexpectedly, I would suggest that you troubleshoot using the steps in this article -
OS X: When your computer spontaneously restarts or displays "Your computer restarted because of a problem." - Apple Support
Thanks for using Apple Support Communities.
Best,
Brett L -
My husband and I both have iPhones. We are currently using the same Apple ID. I have my own ID but when I send him a text message it comes back to me or goes to my computer. How do we separate the 2 ID's so we can send messages?
You need to reconfigure your phone for use with your Apple ID. I suggest you restore yours as new then reconfigure it with your information and Apple ID. Be sure to disable Find My Phone, if it's enabled, before proceeding.
Locked Out, Forgot Lock or Restrictions Passcode, or Need to Restore Your Device: Several Alternative Solutions
A
1. iOS- Forgotten passcode or device disabled after entering wrong passcode
2. iPhone, iPad, iPod touch: Wrong passcode results in red disabled screen
3. Restoring iPod touch after forgotten passcode
4. What to Do If You've Forgotten Your iPhone's Passcode
5. iOS- Understanding passcodes
6. iTunes 10 for Mac- Update and restore software on iPod, iPhone, or iPad
7. iOS - Unable to update or restore
Forgotten Restrictions Passcode Help
iPad,iPod,iPod Touch Recovery Mode
You will need to restore your device as New to remove a Restrictions passcode. Go through the normal process to restore your device, but when you see the options to restore as New or from a backup, be sure to choose New.
You can restore from a backup if you have one from BEFORE you set the restrictions passcode.
Also, see iTunes- Restoring iOS software. -
I have an iPhone 5 and when i lightly tap on the back top part of my phone it sounds like something is rattling inside. Only on the top part of the phone, not on the bottom. I am wondering if anyone knows what this may be and should i have it looked at?
Definitlely have it looked at. That could be a hardware deffect on the maker(apple) so there could be a definite replacement option.
-
I have two Airport Expresses running at my house. Both are connected to different types of speakers. Neither speaker set has its own equalizer, and both require a different EQ setting to sound just right. Does anyone know of a way to have iTunes automatically change its EQ setting based on which Airport Express it is streaming to?
I'm in the same boat. I have a 6 zone amplifier running with 6 airport expresses to different rooms inside and outside the house. Each has it's own acoustic characteristics. I'd really love to be able to set each airport to equalize based on my SPL frequency sweep I did for each room.
Setting the eq in itunes won't do it as that is global plus I have many more sources for auiod other than itunes that use the AEs directly. -
hi guys i have an ipad 1 and when i plug it in to my macbook pro picasa opens up and show its name but when i open itunes i cant find the name, ive been working my head out to solve the problem please help me:(
Pay no attention to iinami, the amount of replies to people saying their handsets must have been jailbroken everytime iTunes throws out an error is tremendous. (Clearly you don't need to have any real knowledge to get to level 3 on these forums, let's hope apple's geniuses know a lot more than some of their customers.)
http://support.apple.com/kb/TS3694
Solution below.
Error 9
This error occurs when the device unexpectedly loses its USB connection with iTunes. This can occur if the device is manually disconnected during the restore process. This issue can be resolved by performing USB troubleshooting, using a different USB dock-connector cable, trying another USB port, restoring on another computer, or by eliminating conflicts from third-party security software. -
i have just updated my phone its iphone 4 and when it was updated it demanded something like to restore an d mobile went off ,i dont wanna lose all my data so help me out what to do ,Thanks
yeah its really like that you mentined , so what to do now .
current situation is that i havent yet restored it,phone is off asking for connecting to itunes and after connecting it is demanding for restore, so where can i access to backup data ? -
I have a MBP 13 EArly 2011 in my laptop problem is when i shutdown my laptop and again start it its very slow and when i put my laptop on sleep and after five minutes everythings fine.Some body know about this problem???????
You may find this user tip article helpful:
https://discussions.apple.com/docs/DOC-3521
Ciao. -
Hi! I have an iPhone 4s and when I FaceTime the sound is really low. It´s really annoying. When I do phone calls it works, and on my MacBook Pro it works much better on FaceTime than on my phone. Can someone pls help to so solute this? Really need help! Thanks
This sounds like something similar to https://discussions.apple.com/thread/3388112?start=105&tstart=0.
Try turning down the speak volume (System Preferences->Sound->Output->Output Volume) or the microphone gain (System Preferences->Sound->Input->Input Volume). Do this on both sides of the call. -
I have an iPhone 5 and when I go to raise or lower my volume it says my headphones are connected and they are not how do I fix this?
more here
https://discussions.apple.com/thread/1343532?start=315&tstart=0
and here
https://www.google.dk/search?client=opera&q=iphone+stuck+in+headphone+mode&sourc eid=opera&ie=UTF-8&oe=UTF-8 -
I have an iPhone 4S and when I try to add an iCloud account is gives me an error "Cannot Sign In, The maximum number of free accounts have been activated on this iPhone." Is there any way to deactivate an account? I have one for myself, one for my family and one for my work. I had all three on my iphone and then I deleted my work. I wanted to get my own person @me.com account. It wouldn't let me create it from my iphone, so I created it from my Macbook Pro. I am trying to add it to my iphone, but it keeps giving me that error. If there is a way I can deactivate my work one so I can use my @me.com personal account?
Welcome to the Apple Community.
Unfortunately once all the 3 iCloud accounts have been created on your mobile device, you cannot create any more regardless of what you do. You will need to re-use one of the accounts that you have already created or create your new account on another device or computer.
You should however be able to add existing accounts to your device, are you sure you aren't trying to create on rather than simply use an existing one. -
My wife and I share apple id but have our own phones, and now having trouble with imessage
My wife and I share apple id but have our own phones, and now having trouble with imessage. Sharing calenders, apps, music, contacts, etc...all great. But when we imessage each other. Our phones get confused and either not deliver message, or send it to and from itself.
Go to Settings > Facetime and you will see "You can be reached for video calls at:"
This should list your phone number (iPhone) and your email address (probably the gmail one).
And then an option for "Add another email..."
Choose that and enter your @me.com account and it'll send a verification email.
Same for iMessage: Settings > Messages > "Receive at" > Add Another Email
So you can be called by facetime and use iMessage through multiple email accounts yes.
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
-
Can you please resend me the link as something went wrong when I tried to update the plugins.
-
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
-
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