Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Overloaded Constructor

Options
  • 21-01-2005 5:42pm
    #1
    Registered Users Posts: 488 ✭✭


    Anyone tell me how to write an Overloaded constructor in a class in java?


Comments

  • Registered Users Posts: 20,993 ✭✭✭✭Stark


    It's the same as a normal constructor method except with different parameters.

    Eg: default constructor: public Test() {...}

    overloaded constructor: public Test(int a, int b) {...}

    You obviously need at least 2 definitions for the method or else you're not overloading anything.


  • Registered Users Posts: 488 ✭✭lad12


    ok..

    so for this question

    An overloaded constructor for Current Account which takes an Account Number argument only, and uses a default balance of 0.0 and minimum balance of 100.0
    would this do it...

    class CurrentAccount extends BankAccount{
    private double minimum;
    class CurrentAccount(int acctNum, double bal){
    Super(accNum, bal);
    }

    public int accNum(int accountNumber, int Balance){
    Balance = 0.0
    minimum = 100.0
    }
    }


  • Registered Users Posts: 20,993 ✭✭✭✭Stark


    No because the constructor is only supposed to "takes an Account Number argument only"
    and a constructor begins with keyword public, not class

    public CurrentAccount(int acctNum)

    I don't know what your BankAccount class looks like so I can't comment on your constructor. And your int accNum method doesn't make sense, why would a getter be changing the values of things?


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    lad12 wrote:
    An overloaded constructor for Current Account which takes an Account Number argument only, and uses a default balance of 0.0 and minimum balance of 100.0
    From the way this is phrased, I'm guessing that there's also a constructor that takes all three arguements, and then a constructor that takes one arg and uses the default values
    public CurrentAccount(int accNum, double bal, double minBal)
    {
        ...
    }
    
    public CurrentAccount(int accNum)
    {
        this(accNum, 0, 100);  // use other contructor with these default values
    }
    


Advertisement