aco 101: intro to computer science anatomy part 3: the constructor

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

Upload: myron-harrell

Post on 20-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

ACO 101: Intro to Computer Science

Anatomy Part 3: The Constructor

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

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 );

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

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( );......

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

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);......

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

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

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

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

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

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.