When a subclass must have its own fields and when to use its super

When a subclass must have its own fields and when to use fields of its superclass?


Hi, thanks for reading this post!

I would use a specific example to 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 example of bank account, account Bank both SavingsAccount will have a getBalance() method. As a result, they
both need an account balance field. My question is as getBalance() for both classes will occur exactly the same
operation, when I should omit declaring the getBalance() method and the accountBalance in the subclass field, and
When should I include them?

My own thought is when we never instantiate a superclass object (for example, an abstract class), then we place
common fields in the superclass abstract and subclasses have access to these areas via getter/Setter protected
access the fields of the superclass. This is the principle of reuse.

But if you do not need to instantiate a superclass and the superclass needs to maintain its own fields, then
I would need to replicate the getBalance() accountBalance and field method in the subclass.

Is my thinking correct or incorrect?

Thank you in advance for your help!

Eric

Published by: er * August 22, 2011 20:19

ER * wrote:
If SavingsAccount inherits BankAccount.getBalance ()... getBalance () would return accountBalance bank account. This isn't the right result we want.

In fact, I think that this is precisely what you want.

We want getBalance() accountBalance bank account back when we use a BankAccount object and SavingsAccount accountBalance when we use a SavingsAccount object.

I seriously doubt that. I think you are confused a bank account with a customer, who may have multiple accounts.

In all the systems I've ever seen, a SavingsAccount IS - A BankAccount - that is to say, it is a subtype of real. Now, it can contain many other areas ('interest'?), that a normal account would not be 'balance' is not one of them.

Winston

Tags: Java

Similar Questions

  • same 3 buttons & 3 different images, but each must have its own actionscript image?

    Here's my problem. I have 3 frames and 3 buttons. Each button will take you to their respected framework. However, I noticed that I can't have 1 keyframe of the actionsript read the same on these 3 images. Why? because if I jump from frame 1 to frame 2 or 3, then I can't go back. Instead, each image must have its own actionscript. The only thing that would change is the name of the method. I find it tedious. Is that what this is? It felt almost seem like it would be less tedious but even for 3 images, I have to change the name of each method as I build more pictures to navigate to. Check my example below:

    Old Actionscript (1 keyframe for 3 images does not have to go back any frame)

    frame1_btn.addEventListener (MouseEvent.CLICK, handlerFrame1);
    frame2_btn.addEventListener (MouseEvent.CLICK, handlerFrame2);
    frame3_btn.addEventListener (MouseEvent.CLICK, handlerFrame3);

    function handlerFrame1(event:MouseEvent):void {}
    gotoAndStop ("frame1");
    }

    function handlerFrame2(event:MouseEvent):void {}
    gotoAndStop ("frame2");
    }

    function handlerFrame3(event:MouseEvent):void {}
    gotoAndStop ("frame3");
    }

    New Actionscript (3 images key and it works):

    Framework 1

    frame1_btn.addEventListener (MouseEvent.CLICK, handlerFrame1);
    frame2_btn.addEventListener (MouseEvent.CLICK, handlerFrame2);
    frame3_btn.addEventListener (MouseEvent.CLICK, handlerFrame3);

    function handlerFrame1(event:MouseEvent):void {}
    gotoAndStop ("frame1");
    }

    function handlerFrame2(event:MouseEvent):void {}
    gotoAndStop ("frame2");
    }

    function handlerFrame3(event:MouseEvent):void {}
    gotoAndStop ("frame3");
    }

    Frame 2

    frame1_btn.addEventListener (MouseEvent.CLICK, handlerFrame,4);
    frame2_btn.addEventListener (MouseEvent.CLICK, handlerFrame,5);
    frame3_btn.addEventListener (MouseEvent.CLICK, handlerFrame,6);

    handlerFrame4function (event: MouseEvent): void {}
    gotoAndStop ("frame1");
    }

    function handlerFrame5(event: MouseEvent): void {}
    gotoAndStop ("frame2");
    }

    handlerFrame6function (event: MouseEvent): void {}
    gotoAndStop ("frame3");
    }

    Frame 3

    frame1_btn.addEventListener (MouseEvent.CLICK, handlerFrame,7);
    frame2_btn.addEventListener (MouseEvent.CLICK, handlerFrame,8);
    frame3_btn.addEventListener (MouseEvent.CLICK, handlerFrame,9);

    function handlerFrame7(event: MouseEvent): void {}
    gotoAndStop ("frame1");
    }

    function handlerFrame8(event: MouseEvent): void {}
    gotoAndStop ("frame2");
    }

    function handlerFrame9(event: MouseEvent): void {}
    gotoAndStop ("frame3");
    }

    You must not have code in all three settings.  The first bit of code that show you should be sufficient under normal circumstances.  If you have the code on a layer that only has a single keyframe and the buttons on a layer that has a key frame, then the buttons will remain functional.  It is only when you create new instances (keyframes) buttons that you must assign new for them event listeners, but even then the same event handler functions can be shared for each of them as a layer actions extends the length needed for all the buttons.

  • My husband doesn't like the itunes account is under my name.  I have a way that he can have his own name and sign on with her and still exist on the family account?

    My husband doesn't like the itunes account is under my name.  I have a way that he can have his own name and sign on with her and still exist on the family account?  Also, have we HAVE two-step verification?   It's a real pain.

    Yes it can have its own ID and share your content via the family sharing, he needs to sign out of your ID and to create his own, then one of you needs to invite another family sharing.

    You don't need to use 2 verification step, if you want you can disable to my Apple ID

  • Why can't I minimizes exceeding 2 Notepad in FF? I used to be able to minimize the notebooks as much as I wanted to. FF must have made an update and now I can't do that. Is there a setting I can change that will allow me to do?

    1. Question

    Why can't I minimizes exceeding 2 Notepad in FF? I used to be able to minimize the notebooks as much as I wanted to. When I search on a topic I keep several notebook open until I finished. FF must have made an update and now I can't do that. Is there a setting I can change that will allow me to do it again?

    Also at this time, the length of the sentence, I type has changed.
    There is no limit for how far she can extend to the right.

    Help, please. I'm not very irritable and don't understand Computerese.
    Please explain in English every day.
    Thanks in advance,

    bouquet

    Bouquet,

    Great to hear that your problem is finally solved.

    Regarding 2nd query you have raised, it is not possible to have the program running if you turn OFF the computer (it's like saying you want the bulb to keep bright despite off). When stop you the machine, you turn off the power... so there is no way that the current state maintained. When start the computer again the next time it starts on new.

    But there are 2 options that can help you with your problem: -.

    1 SAVE your changes before you close the program, and choose next time when you start the computer

       File ->Open (This way your work is preserved and you need not start afresh next time onwards)
    

    2. instead of turning off the computer, choose to Hibernate. When Hibernate you (and NOT CLOSE) the current state is preserved. This means that if you leave the notebook running he will remain in the same State when you next start your computer the next time.

    PS: as this forum is for the FF to discuss related issues, I propose that for further assistance e-mail me at my personal mail ID [email protected]' rather than discuss it here on this forum

    Now your problem is solved with kindness mark this as closed discussion (marking/choice of appropriate solution helped you)

  • I think I have 2 Apple ID. They each have their own email and they are both mine. How can I fix it?

    I think I have 2 Apple ID. They each have their own email and they are both mine. How can I fix it?

    Here's how it works: you can change your Apple ID email address and your content will move you to your new email address using the same Apple ID. An identifier Apple can not be combined with the ID or another address, it will survive as its own Apple ID yes I know I content purchased under more than one Apple ID and am stuck with it also.

  • How to stop Security Center from microsoft that I have my own firewall and anti virus?

    Original title: Microsoft security center

    How to close the Security Center that I have my own firewall and anti virus?

    Hi waynejackson64,

    Follow these steps:

    (a) click Start, Control Panel, Security Center.

    (b) double click Firewall, and then select Disable the firewall.

    How can I turn on or turn off the firewall in Windows XP Service Pack 2 or later versions?

    http://support.Microsoft.com/kb/283673

  • JavaScript seems to be disabled in your browser... You must have javascript enabled in your browser to use the features of this site... How can I fix it

    JavaScript seems to be disabled in your browser... You must have javascript enabled in your browser to use the features of this site... How can I fix it

    Clear your NoScripts modules.

    Nancy O.

  • When I use my super disc to DVD recording, he plays on my IMac, but not on my dvd player.  I'm something wrong or just not do something?

    When I use my super disc to DVD recording, he plays on my IMac, but not on my dvd player.  I'm something wrong or just not do something?

    what you save? How do you save it? What format? what application? what you're trying to read on?

  • Can I use all 500 GB on a hard drive, I have install windows 7 and it only uses I think 466 around this amount, why is - this

    Can I use all 500 GB on a hard drive, I have install windows 7 and it only uses I think 466 around this amount, why is - this

    Hello Edtannjr,

    Thank you for your message.  Remember that the operating system itself takes place and many OEM systems contains a recovery partition which can take up to a space as well.
    See you soon

    Engineer Jason Microsoft Support answers visit our Microsoft answers feedback Forum and let us know what you think.

  • autologin Windows 7 without box "users must enter a user name and password to use this computer"

    My dialog box user accounts does not the box "users must enter a user name and password to use this computer" and how do I turn it on?

    Hello Russ,

    You can continue with the following steps to support.  However, if your computer is on a domain, unfortunately, there's nothing we can do.  You will need to contact the system/network administrator.  TechNet forums can be beneficial for a resolution on your http://social.technet.microsoft.com/Forums/en-us/category/w7itproinquiry.  Please create a Restore Point and backup of the registry before proceeding.

    1. click on start, run, type regedit, and then click OK.
    2. look for the following registry key:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon
    3. with the help of your account name and password, double-click the DefaultUserName entry, type your user name, and then click OK.
    4. double-click the DefaultPassword entry, type your password in the value data box, and then click OK.

    If there is no DefaultPassword value, create the value. To do this, follow these steps: a. in the registry editor, click on edit, click New, and then click String value.
    b. Type DefaultPassword as the value name, and press ENTER.
    c. double-click the newly created key, and then type your password in the value data box.
    If no DefaultPassword string is specified, Windows XP automatically changes the value of the AutoAdminLogon registry key by replacing 1 (true) to 0 (false) to turn off the AutoAdminLogon feature.
    5 double-click the AutoAdminLogon entry, type 1 in the value data box, and then click OK.

    If there is no AutoAdminLogon entry, create the entry. To do this, follow these steps: a. in the registry editor, click on edit, click New, and then click String value.
    b. type AutoAdminLogon as the value name, and press ENTER.
    c. double-click the newly created key, and then type 1 in the value data box.
     
    6. exit the registry editor.
    7. click on start, click on restart and then click OK.

    I hope that this information is beneficial

    Thank you

    Aaron
    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think

  • I just download WOS vol 1 and when I open it it says that I must have directx sound system, and I don't have the audio drivers?

    I am a lover of big train and waited for a game like this since the first microsoft train sim please help

    Hi danwhittaker,

    To better assist you, please provide the following details:

    (1) what WOS - game to play?

    (2) what is the complete error message you receive?

    (3) what edition of Windows you are using on the computer?

    (4) what is the number of brand and model of your computer?

    I would like to know more information about the game you are trying to play

    Let us know the version of Windows that you are using on the computer.

    What version of the operating system Windows am I running?

    http://Windows.Microsoft.com/en-us/Windows7/help/which-version-of-the-Windows-operating-system-am-i-running

    Thanks and greetings

    Ajay K

    Microsoft Answers Support Engineer
    ***************************************************************************

  • I was cleaning my computer deleating some files... I must have its deleated how it back

    Not sure of all the files that were deleated

    Hello

    · What is the number and the model of the computer?

    You can follow the steps from the link below: no sound in Windows: http://windows.microsoft.com/en-US/windows/help/no-sound-in-windows

  • I have the Iphone 3Gs and you are using Windows XP. I can't add APPs on my phone, it fails when I have Windows 2000 to insert disk. I have XP Home 2002, so I don't have a disc of 2000.

    IPhone 3gs and Windows XP

    Hi JenniferNevada,

    Thanks for posting. Have you tried to reinstall I-Tunes? Also can you give me more details on the Windows 2000 disk and exactly when you insert in your XP machine for I-Tunes work properly? In addition, you can check with the Apple community because they may have ideas with this app that we don't have. http://discussions.Apple.com/index.jspa

    I hope this helps! Shawn - Support Engineer - MCP, MCDST
    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think

  • Must have access to Java SE 6 for use in CS4 on OS 10.8.5. (PS starts without him)

    Where can I access Java SE 6 reliable for use in CS4 on OS 10.8.5.   Right now PS does not open without it (I get a warning message saying install the Java SE 6 runtime.  When I hit install, the result is no fle found.

    Adobe has a download dead related or similar archivel/WASTE plugins fault a clean download avoid malware.

    This will work even if I get a clean version.  It should... I have CS4 running on an another iMac with 10.8.5

    If it won't work... What are the alternatives.  I can't easily or usefully uninstalls 10.8.5 and return to 10.6.

    Any help is appreciated.

    Thank you.

    Jerry

    Have you tried Apple? Java for OS X 2014-001

    Benjamin

  • guest computer must have access to the printer host cannot use

    I have a new Mac, an old Mac, an old Windows machine and printer of Xante AW1200.

    Almost all of my work is done on the new Mac.  I get the old machine Mac or Windows of the drawer on rare occasions, or when I need to print something, because the new Mac cannot use the printer.  (Xante never taken the trouble to develop an OS X 10.6.4 driver for this printer.)

    I would like to get rid of the Windows machine.  I have it successfully migrated to a machine of comments on the new Mac, using vmware Fusion 3.1.3 but I have not found a way to get the computer to see the printer.

    This isn't a complete surprise, as Xante does not provide a driver for their (former) printer and (my new host) operating system OS X 10.6.4 - but they were planning a WIndows XP driver that has always worked well on the Windows machine, connected by the RS232 port.

    The printer also works very well on my old Mac, connected via ethernet.

    Can I connect the printer to the new Mac's ethernet port.  Of course, the Mac cannot work with it.  But could the guest computer use Windows sound driver to manage the printer?

    None of the four standard ways of printing seem applicable: ThinPrint because the host cannot use the printer.  USB because the printer is not a USB connector.  network / wireless because I do not have my old Mac running all the time.  Hello because I think that it requires that the host can see the printer.

    It certainly isn't a frequently asked Question.  Sorry, my situation is so unusual.  But the printer is special enough for the work that I do.  Any tips?

    Thank you.

    If you have a router on your site then you should be able to connect the printer to the router, configure it as needed, and then accessing it in the comments as a network printer.  Note: The guest network should be configured as connected by a bridge to access in this way.

    Another option of network which is doable, but a little more difficult to set up, is to fix the printer directly on the Mac with a standard CAT5 Ethernet cable, not a Crossover as suggested in the Ethernet Port of Mac being automatic detection User Guide.  You would then manually configure the IP address, etc. on the printer and network bridged on the comments and even if Bridged his only access to the printer on this network adapter.  Then use a second network in the guest configured as NAT, if you do not also use Bridged on that, to enable the client to have access to other resources on the physical LAN to the host, such as Internet access, etc..

Maybe you are looking for

  • MacBook Pro 8.2 - almost hourly kernel panic leading to restarts

    I was faced with frequent kernel panics in the last month or so and took my MBP to an Apple authorized service provider, which says that the problem is with my logic board but couldn't provide me with a diagnostic report of my machine that said the B

  • Firefox crashes on Windows 8

    I'm going to do common things (Facebook and youtube recently) and firefox randomly crash all the ~ 15 minutes more or less. I've had this computer and windows 8 during about two months and had no problems before, Internet Explorer still works fine an

  • iMessage not activate my phone number

    I have an iPhone 5 c updated to iOS 9.3. When I go to activate facetime or iMessage, they work properly with my email, but I have my phone number does not show under "send and received. Ive tried everything I was suggested. for example, I have reset

  • I'm an electronic distortion: a kind of click/Snap/pop looks like a loose wire connection or his striped vinyl on my recordings.

    Hello. Day One - just joined. I love Garageband (for iPad) and are now working with her for about two months with some success. I use the iTrack Solo for voice and guitar Focusrite and a Midi for keyboards iRig. Recently, I have an electronic distort

  • interfacing of labview with relays

    Hello Could someone please tell me how could interface labview in a relay at the hardware level external to the switch of a circuit path. Thanks in advance... Concerning