Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Overloaded Constructor

  • 21-01-2005 05:42PM
    #1
    Registered Users, Registered Users 2 Posts: 488 ✭✭


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


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 21,262 ✭✭✭✭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, Registered Users 2 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, Registered Users 2, Paid Member Posts: 21,262 ✭✭✭✭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