How do I reference an object passed through a method?

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How do I reference an object passed through a method?

  1. #1
    Join Date
    Jan 2005

    How do I reference an object passed through a method?

    This is hopefully a pretty simple one. I've got an abstract superclass (Account) that defines the generic behavior of a bank account. One of the methods (non abstract) is to transfer funds from one account to another. My set up so far is this:

    public void transfer(double transferAmount, Account transferToAccount) {
    		if(balance-transferAmount<0.00) {
    			System.out.println("Insufficient Funds - transfer denied");
    		else if (balance-transferAmount>=0.00) {
    			transferToAccount.balance += transferAmount;
    			System.out.println("Transfer Complete");
    The problem is that the balance variable (which should refer to the variable balance of the instance the money is being transferred from) is being initialized at 0.0d - which is the value I assigned to it in the superclass.

    On top of that, the transferToAccount methods aren't working - they are all initialized at the value of the superclass variable as well. I don't know how to get to these instances. Does that make sense?

    Thanks in advance

  2. #2
    Join Date
    Nov 2004
    It is difficult to pinpoint the bug(s) without the code of the abstract class and the
    descendant(s). If the metod you've shown here is defined non-abstract in the
    abstract superclass, and is invoked using the abstract class, then you should define
    this method as abstract in the abstract superclass also.

    But I cannot be sure without all the code in view...
    eschew obfuscation

  3. #3
    Join Date
    Jan 2005
    D'oh - looks like I had a bit of a Homer moment. The problem was that my original transfer method was hard coded between 2 specific account objects, making the "app" useless for expansion. I was told to a) make the transfer behavior more generic, and b) make the method more expandable.

    So I took your advice and made the transfer method abstract in the abstract superclass. Then in the two subclasses, I overrode that method with more spcific behavior, but changed the methods using the Account transerToAccount as a parameter.

    It seems to work now - I'll double check it later when my head has cleared and the steam has stopped coming out of my ears

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center