Presentation is loading. Please wait.

Presentation is loading. Please wait.

ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.

Similar presentations


Presentation on theme: "ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor."— Presentation transcript:

1 ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor

2 What the constructor does To set attributes when an object is created, you define a constructor. A constructor is called when an object is created, examples: BankAccount ku = new BankAccount( ); BankAccount ku = new BankAccount( "KU", 1234567890 );

3 Constructor A constructor is a method that is called when a new object is created. A constructor has the same name as the class. The constructor usually initializes the attributes (state) of the object. public class BankAccount { private long accountID; private String name; private double balance; /** constructor */ public BankAccount( ) { accountID = "unknown"; balance = 0; } public double myMethod( ) { BankAccount acct;... acct = new BankAccount( );...

4 Constructor (2) A class can have several constructors. The code will use the one that matches the arguments in the "new" command. If a matching constructor is not found, it is an error! public class BankAccount { private long accountID; private String name; private double balance; /** constructor */ public BankAccount( String aName, long id ) { name = aName; accountID = id; balance = 0; } public double myMethod( ) { BankAccount acct;... acct = new BankAccount( "T. Shinawat", 12345678);...

5 Writing Constructors (1) public class BankAccount { private long accountID; private String name; private double balance; /* default constructor */ public BankAccount( ) { accountID = 0; name = "unknown"; balance = 0; } /* parameterized constructor */ public BankAccount(String aname, long id) { name = aname; accountID = id; balance = 0; } No return value -- not even "void" ! The default constructor doesn't have any parameters

6 Writing Constructors (2) public class BankAccount { private long accountID; private String name; private double balance; /* default constructor */ public BankAccount( ) { accountID = 0; name = "unknown"; balance = 0; } /* a parameterized constructor */ public BankAccount(String aname, long id) { name = aname; accountID = id; balance = 0; } Initialize the attributes using parameter values Parameterized Constructors: A class can have many constructors, each with different parameters. This is called overloading a constructor

7 Writing Constructors (3) /** default constructor */ public BankAccount( ) { this("unknown“, 0); } /* a parameterized constructor */ public BankAccount(String aname, long id) { name = aname; accountID = id; balance = 0; } Constructor can call another constructor. A constructor can call another constructor using “this( … )”.  “this(…)” must be the first statement in the constructor.  This is useful for eliminating duplicate code.


Download ppt "ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor."

Similar presentations


Ads by Google