Sunteți pe pagina 1din 136

Module 7: Objects

*
Ron K. Cytron

*
Department of Computer Science and Engineering
Washington University in Saint Louis
Thanks to Alan Waldman
for comments that improved these slides

Prepared for 2u
Semester Online

Copyright Ron K. Cytron 2013


Monolog 0
7.0 Introduction
So far, we have studied
data types, conditional and iterative execution
methods for computational abstraction (verbs)
Relative low level
We meet the the computer at its level
Why not get it to meet us at our level?
What does this mean?
Make abstractions about nouns
robot.turn(90)
Create instances of related nouns
account1, account2,
Remember information between method calls
position of robot
balance in an account
Objects allow us to do this
2
7.0 Introduction

Java is an object-oriented language


But our introduction to this language was more
concerned with building basic knowledge and skills
We now use what we know to design and instantiate
objects
We have already seen and used some objects:
Robot, Color
String (!)
Feels like a primitive type (int, double)
But each String is actually an object

3
Monolog 0
7.0 Introduction
Example we will use
Account (as in bank account)
Each account has a balance
Each account has its own interest rate
Why do I need objects? Can't I do this without them?
Yes!
int[] accountBalances (or int accountBalance1, ..,)
double[] accountInterestRates (or double
accountInterestRate1)
Problems?
An account is the "same index" used in both of those arrays. It
would be easy to make a mistake indexing the arrays. We need
a better organization that brings an account's balance and
interest rate together in one object.
The data in the arrays is open for change anywhere in the
program. I want to control access so that an account balance
changes by depositing money.

4 End of Monologue
Oyster 1
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations
Methods
! !private int balance;! Braces for scope

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
5
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations (names)
Methods
! !private int balance;! Braces for scope

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
6
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations
Methods
! !private int balance;! Braces for scope

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
7
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations
Methods
! !private int balance;! Braces for scope

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
8
7.1 Overview of an object
We see new elements
public class Account {!
private
no return type
! !private int balance;! no use of static

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
9
7.1 Overview of an object
We see new elements
public class Account {!
private
no return type
! !private int balance;! no use of static

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
10
7.1 Overview of an object
We see new elements
public class Account {!
private
no return type
! !private int balance;! no use of static

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
11
7.1 Overview of an object
We see new elements
public class Account {!
private
no return type
! !private int balance;! no use of static

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
12
7.1 Overview of an object
We next look at these
public class Account {! elements, one by one
Keep in mind the purpose
! !private int balance;! of a bank account

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
13
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
14
7.1 Overview of an object
public class Account {!

! This specifies
!private the
int balance;! name of the object
type.
! !public Account(int initialBal) {!
! !
Style: Object type names always begin
!this.balance = initialBal;!
! !}! with an upper-case letter
! Noint
!public spaces or punctuation
getBalance() {! in the class
! ! name,
!returnbut CamelCasing is used
this.balance;!
! !}! Robot, Color, MouseListener!
! !public void deposit(int funds) {!
! !
The!this.balance
name must=agree with the filename
this.balance + funds;!
! !}!
shown in the package explorer

}!
15
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
16
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
The constructor has the{!same name as the
! ! object type.this.balance;!
!return
! It is like any other method
!}!
! Can
!public havedeposit(int
void parameters funds) {!
! ! But it has no return
!this.balance = type
this.balance + funds;!
! It is responsible for giving birth to the object
!}!
Initializing instance variables
}!
17
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
The constructor has the{!same name as the
! ! object type.this.balance;!
!return
! It is like any other method
!}!
! Can
!public havedeposit(int
void parameters funds) {!
! ! But it has no return
!this.balance = type
this.balance + funds;!
! It is responsible for giving birth to the object
!}!
Initializing instance variables
}!
18
7.1 Overview of an object
Although no return type is declared
public class Account {! The method does return an
instance of the declared
object type
! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
The constructor has the{!same name as the
! ! object type.this.balance;!
!return
! It is like any other method
!}!
! Can
!public havedeposit(int
void parameters funds) {!
! ! But it has no return
!this.balance = type
this.balance + funds;!
! It is responsible for giving birth to the object
!}!
Initializing instance variables
}!
19
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
The constructor has the{!same name as the
! ! object type.this.balance;!
!return
! It is like any other method
!}!
! Can
!public havedeposit(int
void parameters funds) {!
! ! But it has no return
!this.balance = type
this.balance + funds;!
! It is responsible for giving birth to the object
!}!
Initializing instance variables
}!
20
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! !
How do you find an object's
!return this.balance;!
! !}!constructors?
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
21
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! !
How do you find an object's
!return this.balance;!
! !}!constructors?
! Look
!public voidfor a method with
deposit(int the {!
funds) same name as
! ! the object type= this.balance + funds;!
!this.balance
! !}! It has no return type

}!
22
7.1 Overview of an object
public class Account {!

! !private int balance;!


// Open an account with an initial balance!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
How do you find an object's
! ! !return this.balance;!
! !}!
constructors?
! Look
!public voidfor a method with
deposit(int the {!
funds) same name as
! !
the object type
!this.balance = this.balance + funds;!
! !}! It has no return type
There can be more than one constructor!
}!
23
7.1 Overview of an object
public class Account {! This kind of constructor (no
parameters) is called a default
constructor
! !private int balance;!
// Open an account with zero balance!

! !public Account() {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
How do you find an object's
! ! !return this.balance;!
! !}!
constructors?
! Look
!public voidfor a method with
deposit(int the {!
funds) same name as
! !
the object type
!this.balance = this.balance + funds;!
! !}! It has no return type
There can be more than one constructor!
}!
24
7.1 Overview of an object
public class Account {! The signature of this constructor is
different than Account(int)
This is how they are differentiated
! !private int balance;!
// Open an account with zero balance!

! !public Account() {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
How do you find an object's
! ! !return this.balance;!
! !}!
constructors?
! Look
!public voidfor a method with
deposit(int the {!
funds) same name as
! !
the object type
!this.balance = this.balance + funds;!
! !}! It has no return type
There can be more than one constructor!
}!
25
7.1 Overview of an object
public class Account {! This signature differs from the
other two as well
! !private int balance;!
// Open an account by transfer of funds!

! !public Account(Account closing) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
How do you find an object's
! ! !return this.balance;!
! !}!
constructors?
! Look
!public voidfor a method with
deposit(int the {!
funds) same name as
! !
the object type
!this.balance = this.balance + funds;!
! !}! It has no return type
There can be more than one constructor!
}!
26
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
27
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public
InstanceAccount(int
variables initialBal) {!
are usually declared private
! ! !this.balance = initialBal;!
So that they can be accessed only within a
! !}!
method
! !public int getBalance() {!
! They
! live "between
!return the methods"
this.balance;!
! !}! Methods can read and write them
! Their
!public values
void persist after
deposit(int funds)methods
{! return
! !
They !this.balance = this.balance
are the "has-a"s + funds;!
of object oriented design
! !}!(more on this later)

}!
28
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public
InstanceAccount(int
variables initialBal) {!
are usually declared private
! ! !this.balance = initialBal;!
So that they can be accessed only within a
! !}!
method
! !public int getBalance() {!
! They
! live "between
!return the methods"
this.balance;!
! !}! Methods can read and write them
! Their
!public values
void persist after
deposit(int funds)methods
{! return
! !
They !this.balance = this.balance
are the "has-a"s + funds;!
of object oriented design
! !}!(more on this later)

}!
29
7.1 Overview of an object
public class Account {!
// An Account has-a balance!
! !private int balance;!

! !public
InstanceAccount(int
variables initialBal) {!
are usually declared private
! ! !this.balance = initialBal;!
So that they can be accessed only within a
! !}!
method
! !public int getBalance() {!
! They
! live "between
!return the methods"
this.balance;!
! !}! Methods can read and write them
! Their
!public values
void persist after
deposit(int funds)methods
{! return
! !
They !this.balance = this.balance
are the "has-a"s + funds;!
of object oriented design
! !}!(more on this later)

}!
30
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
31
7.1 Overview of an object
public class Account {! Recall
Constructor is obligated to
! !private int balance;! initialize the instance
variables

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
32
7.1 Overview of an object
public class Account {! Recall
Constructor is obligated to
! !private int balance;! initialize the instance
variables

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
This assignment statement {!initializes the instance
! ! variable
!return this.balance;!
balance!
! !}! The value for initialization is provided as a
! !public void deposit(int
parameter funds) {!
to the constructor
! ! new!this.balance
Account(100) = this.balance + funds;! to
causes this constructor
! !}! initialize the account balance to 100

}!
33
7.1 Overview of an object
public class Account {! new Account(100)!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
This assignment statement {!initializes the instance
! ! variable
!return this.balance;!
balance!
! !}! The value for initialization is provided as a
! !public void deposit(int
parameter funds) {!
to the constructor
! ! new!this.balance
Account(100) = this.balance + funds;! to
causes this constructor
! !}! initialize the account balance to 100

}!
34
7.1 Overview of an object
public class Account {! new Account(100)!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance()
This assignment statement {!initializes the instance
! ! variable
!return this.balance;!
balance!
! !}! The value for initialization is provided as a
! !public void deposit(int
parameter funds) {!
to the constructor
! ! new!this.balance
Account(100) = this.balance + funds;! to
causes this constructor
! !}! initialize the account balance to 100

}!
35
7.1 Overview of an object
public class Account {! new Account(100)!

! !private int balance;!

! !public Account(int initialBal) {!


! ! 100
!this.balance = initialBal;!
! !}!
! !public int getBalance()
This assignment statement {!initializes the instance
! ! variable
!return this.balance;!
balance!
! !}! The value for initialization is provided as a
! !public void deposit(int
parameter funds) {!
to the constructor
! ! new!this.balance
Account(100) = this.balance + funds;! to
causes this constructor
! !}! initialize the account balance to 100

}!
36
7.1 Overview of an object
public class Account {! new Account(100)!
The instance variable balance
! !private int balance;! now has value 100, reflecting the
creation of this new Account!

! !public Account(int initialBal) {!


! ! 100
!this.balance = initialBal;!
! !}!
! !public int getBalance()
This assignment statement {!initializes the instance
! ! variable
!return this.balance;!
balance!
! !}! The value for initialization is provided as a
! !public void deposit(int
parameter funds) {!
to the constructor
! ! new!this.balance
Account(100) = this.balance + funds;! to
causes this constructor
! !}! initialize the account balance to 100

}!
37
7.1 Overview of an object
public class Account {!

! !private int balance;! (100)

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
Instance variables retain their values
! ! !return this.balance;!
! !}!
So when the constructor returns, the
balance is still 100
! !public void deposit(int funds) {!
! ! The balance value
!this.balance persists and
= this.balance any
+ funds;!
! !}! method in Account can access or change it

}!
38
7.1 Overview of an object
References to instance variables
public class Account {! Can occur in any (non-static)
method
! !private int balance;! Use the current value of the
instance variable

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
39
7.1 Overview of an object
References to instance variables
public class Account {! Can occur in any (non-static)
method
! !private int balance;! Use the current value of the
instance variable

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
40
7.1 Overview of an object
References to instance variables
public class Account {! Can occur in any (non-static)
method
! !private int balance;! Use the current value of the
instance variable

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
41
7.1 Overview of an object
Instance variables are usually
public class Account {! preceded by "this."
this.balance!
Distinguishes them from local
! !private int balance;! variables, such as initialBal!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
42
7.1 Overview of an object
How do you find the
public class Account {!
instance variables of an
object?
! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
43
7.1 Overview of an object
Instance variables
Are declared outside of any method
public class Account {!
Usually have the word private in their
declaration
Usually start with "this."!
! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
44
7.1 Overview of an object
public class Account {!
We write methods to provide functionality for the object
! !private
These methods can use the instance variables as
int balance;!
needed, or change their value
! StaticAccount(int
!public methods cannot access instance
initialBal) {! variables
They have signatures, return types, and code just like
! ! the methods
!this.balance = initialBal;!
we have studied so far
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
45
7.1 Overview of an object
public class Account {!
This method returns the value of the instance
! !private
variable int balance;!
balance
When method serves only to return the value
! !public Account(int initialBal) {!
of an instance variable, we call it an accessor,
! ! !this.balance = initialBal;!
or more specifically, a getter
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
46
7.1 Overview of an object
public class Account {!
A call to getBalance()
returns the current value
! !private int balance;! of the instance variable
balance!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
47
7.1 Overview of an object
public class Account {!
A call to getBalance()
returns the current value
! !private int balance;! of the instance variable
balance (say, 100)!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
48
7.1 Overview of an object
public class Account {!
A call to getBalance()
returns the current value
! !private int balance;! of the instance variable
balance (say, 100)!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
100
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
49
7.1 Overview of an object
public class Account {!
A call to getBalance()
returns the current value
! !private int balance;! of the instance variable
balance (say, 100)!
!
100 !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
100
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
50
7.1 Overview of an object
public class Account {!

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
51
7.1 Overview of an object
public class Account {!
This method serves to deposit money into an
! !private
account.int balance;!
It does this by changing the value of the
! !public Account(int initialBal) {!
balance instance variable
! ! !this.balance = initialBal;!
! !}!
The code increments this object's balance
! by the specified
!public value of funds.
int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
52
7.1 Overview of an object
public class Account {!
Suppose balance is
currently 100, and we
! !private int balance;! ask this Account to
deposit 50!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
53
7.1 Overview of an object
public class Account {!
Suppose balance is
currently 100, and we
! !private int balance;! ask this Account to
deposit 50!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
50
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
54
7.1 Overview of an object
public class Account {!
Suppose balance is
currently 100, and we
! !private int balance;! ask this Account to
deposit 50!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
50
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! 50
!this.balance = this.balance + funds;!
! !}!

}!
55
7.1 Overview of an object
public class Account {!
Suppose balance is
currently 100, and we
! !private int balance;! ask this Account to
deposit 50!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! 100
!this.balance = this.balance 50
+ funds;!
! !}!

}!
56
7.1 Overview of an object
public class Account {!
Suppose balance is
currently 100, and we
! !private int balance;! ask this Account to
deposit 50!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! 100
!this.balance = this.balance 50
+ funds;!
! !}!

}!
57
7.1 Overview of an object
public class Account {!
Suppose balance is
currently 100, and we
! !private int balance;! ask this Account to
deposit 50!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! 100 150 + funds;!
!this.balance = this.balance 50
! !}!

}!
58
7.1 Overview of an object
public class Account {! Suppose balance is
currently 100, and we ask
! !private int balance;! this Account to deposit 50
Now balance is 150!
! !public Account(int initialBal) {!
! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! 100 150 + funds;!
!this.balance = this.balance 50
! !}!

}!
59
7.1 Overview of an object
public class Account {!
The value persists after
the method returns!
! !private int balance;!(150)

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
60
7.1 Overview of an object
public class Account {! Class name

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
61
7.1 Overview of an object
public class Account {! Instance variables

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
62
7.1 Overview of an object
public class Account {! Constructors

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
63
7.1 Overview of an object
public class Account {! Methods

! !private int balance;!

! !public Account(int initialBal) {!


! ! !this.balance = initialBal;!
! !}!
! !public int getBalance() {!
! ! !return this.balance;!
! !}!
! !public void deposit(int funds) {!
! ! !this.balance = this.balance + funds;!
! !}!

}!
64 End of Oyster
BLT public 2
7.2 Exercise

Video intro. All following are for question card


Showing the previous code, I'd like to have
students identify
The constructor
The instance variable(s)
An accessor
A method that changes the value of an instance
variable
Suppose the balance of an account represents
dollars, and it costs one dollar to find out your
balance.
Modify the getBalance() method appropriately
65
7.2 Exercise

Video response
Show solution

66 End of BLT
Oyster 3
7.3 How to make and use objects
Account acct1 = new Account(100);!

Declarations are just like type types we have


already studied:
type name = initial value
This is just like declarations we have seen
before!
67
Oyster 3
7.3 How to make and use objects
int size = 300;!

This is an int variable named size!


It can take on any int's value
It happens to be initialized to 300!
But it could change to some other value later

Declarations are just like type types we have


already studied:
type name = initial value

68
Oyster 3
7.3 How to make and use objects
Account acct1 = new Account(100);!

This is an Account variable named acct1!


It can take on any Account's value
It happens to be initialized so that it references
new Account(100)!
But it could reference some other Account later

Declarations are just like type types we have


already studied:
type name = initial value!
Let's break this down step by step

69
7.3 How to make and use objects
Account acct1 = new Account(100);!

Declarations are just like type types we have


already studied:
type name = initial value!
By saying new Account() we run the
constructor for the Account object
70
7.3 How to make and use objects
Account acct1 = new Account(100);!

Java looks for a constructor with the


signature of an int parameter. Recall:

public Account(int initialBalance) {!


!this.balance = initialBalance;!
}!

Declarations are just like type types we have


already studied:
type name = initial value!
By saying new Account() we run the
constructor for the Account object
71
7.3 How to make and use objects
Account acct1 = new Account(100);!

Java looks for a constructor with the


signature of an int parameter. Recall:

public Account(int initialBalance) {!


!this.balance = initialBalance;!
100
}!

Declarations are just like type types we have


already studied:
type name = initial value!
By saying new Account() we run the
constructor for the Account object
72
7.3 How to make and use objects
Account acct1 = new Account(100);!

The result of this operation is a new Account


object whose balance instance variable is
initialized to 100!

Declarations are just like type types we have


already studied:
type name = initial value!
By saying new Account() we run the
constructor for the Account object
73
7.3 How to make and use objects
Account acct1 = new Account(100);!

By saying new Account() we run the


constructor for the Account object
The result of the constructor's call is returned
That value is captured in acct1, which now
references the constructed object!
74
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

Now there are two objects


acct1 has an initial balance of 100
acct2 has an initial balance of 50
These are separate objects and they cannot
change or reference each other

75
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

public Account(int initialBalance) {!


! this.balance = initialBalance;!
}!

Common analogy:
Constructor is a cookie cutter

76
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

public Account(int initialBalance) {!


! this.balance = initialBalance;!
}!

Common analogy:
Constructor is a cookie cutter

images from http://www.kellerink.com/blog/cookie-cutter-solutions-successful-startup 77


7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

Common analogy:
Constructor is a cookie cutter
The constructed objects are cookies

78
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

50

Common analogy:
Constructor is a cookie cutter
The constructed objects are cookies

79
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

Common analogy:
Constructor is a cookie cutter
The constructed objects are cookies

80
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

We will next run a method on the object


referenced by acct1.

81
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Runs the deposit method on the object


referenced by acct1.

82
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Syntax
Mention an object reference, followed by a
dot

83
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Syntax
Mention an object reference, followed by a
dot
Then mention a method defined for that
object, with the parameters it needs
84
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Throughout, this is
referring to acct1!

Recall:
public void deposit(int funds) {!
this.balance = this.balance + funds;!
}!

85
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Recall:
public void deposit(int funds) {!
this.balance = this.balance + funds;!
}!

86
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Recall:
public void deposit(int funds) {!
this.balance = this.balance
100 + funds;!
}!

87
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Recall:
public void deposit(int funds) {!
this.balance = this.balance
100 + funds;!
}!

88
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Recall:
public void deposit(int funds) {!
this.balance = this.balance
100 + funds;!
35
}!

89
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!

acct1.deposit(35);!

Recall:
public void deposit(int funds) {!
this.balance = this.balance
135 + funds;!
}!

90
7.3 How to make and use objects
Account acct1 = new Account(100);!
135
Account acct2 = new Account(50);!

acct1.deposit(35);!

Recall:
public void deposit(int funds) {!
this.balance = this.balance
135 + funds;!
}!

91
7.3 How to make and use objects
Account acct1 = new Account(100);!
135
Account acct2 = new Account(50);!

acct1.deposit(35);!

Runs the deposit method on the object


referenced by acct1.
Result is that the balance increases to 135
The method is void, so it returns nothing

92
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Running getBalance() on acct1 returns


that account's balance, as held in its balance
instance variable!

93
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Causes the following message to print:


Balance 1 135!

94
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Account acct3 = new Account(acct2.getBalance()*3);!

Evaluated first, returns 50

95
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Account acct3 = new Account(acct2.getBalance()*3);!

Evaluated first, returns 50


50 * 3 = 150

96
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Account acct3 = new Account(acct2.getBalance()*3);!

Evaluated first, returns 50


50 * 3 = 150
Constructor is run on 150

97
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!
150

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Account acct3 = new Account(acct2.getBalance()*3);!

Evaluated first, returns 50


50 * 3 = 150
Constructor is run on 150 (another cookie cut)
Result is now referenced by acct3!

98
7.3 How to make and use objects
Account acct1 = new Account(100);!
Account acct2 = new Account(50);!

acct1.deposit(35);!
System.out.println("Balance 1 " + acct1.getBalance());!

Account acct3 = new Account(acct2.getBalance()*3);!


System.out.println("Balance 3 " + acct3.getBalance()); !

Prints the following


Balance 3 150

99 End of Oyster
Roundtable
7.3b Roundtable

I tell a story, and the user instantiates objects


on the screen

100 End of Roundtable


Oyster 4
7.4 How are objects stored?

Stack (review)
Like a stack of plates in the cafeteria
Parameters are pushed onto the stack for a method
call
foo(5,7): 5 is pushed and then 7
The method pops the values off the stack and
assigns them to its parameters
For foo(a,b), a is assigned 5, b is assigned 7
The method executes up to a return statement
The value returned by the method is pushed onto the
stack
The caller retrieves the value
x = foo(a,b): the value computed by foo is popped
from the stack and assigned to x
101
7.4 How are objects stored?

Stack (review)
Like a stack of plates in the cafeteria
Heap (new)
Objects are allocated in the heap
Objects live there until they can no longer be
referenced
After they die, they are considered garbage and are
collected by the garbage collector
The space allocated for an object includes room for
its instance variables

102
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap

acct1! 100

103
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap

acct1! 100

104
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap

acct1! 100

The local variable acct1 now


references the Account object
allocated in the heap
Another way to think of this is
as follows..

105
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap

acct1! 100
348

Every object in the heap is


situated at some address
In this case, the Account object
is allocated at address 348

106
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap

acct1! 100
348
348

Every object in the heap is


situated at some address
In this case, the Account object
is allocated at address 348
The acct1 variable therefore
contains 348

107
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap
Account acct2 = new Account(50);!

100

acct1!

Space is made in the 50


heap for the second
allocation acct2!

108
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap
Account acct2 = new Account(50);!
acct1! 100
348
348

acct2!
50
408 408

109
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap
Account acct2 = new Account(50);!
acct1! 100
348
348

acct2!
50
408 408

110
7.4 How are objects stored?

Heap

Each object allocated in the 100


348
heap has room for all of its
instance variables

50
408
public class Account {!

! !private int balance;!

}!
111
7.4 How are objects stored?

Heap

Each object allocated in the 100


heap has room for all of its 348
.05
instance variables
The more instance variables, the
more space is needed for each
object 50
408
.07
public class Account {!

! !private int balance;!


! !private double interestRate;!

}!
112
7.4 How are objects stored?

Heap

The constructor should be 100


348
modified to take in an interest .05
rate and store it as the instance
variable

50
408
.07
public class Account {!
public Account(int iBal, double r) {!
!this.balance = iBal;!
!this.interestRate = r;!
}!
}!
113
7.4 How are objects stored?

Heap

The constructor should be modified 100


to take in an interest rate and store 348
.10
it as the instance variable
Or, perhaps the interest rate could
be computed from the initial
balance? 50
408
.05
public class Account {!
public Account(int iBal) {!
!this.balance = iBal;!
!this.interestRate = iBal/1000.0;!
}!
}!
114
7.4 How are objects stored?

Heap

These are issues of design 100


348
Satisfying the method's .05
specification

50
408
.07
public class Account {!
public Account(int iBal) {!
!this.balance = iBal;!
!this.interestRate = iBal/1000.0;!
}!
}!
115 End of Oyster
7.4b Exercises with object placement
Roundtable

Go through about 10 lines of code


Drawing pointers
Making up addresses for things

116 End of Roundtable


Oyster 5
7.5 Object design

We have declared and used Account objects


How are such objects designed?
User story: describes the object in terms of its
properties and the functionality it offers
We respond to the user story by defining
Instance variables, hidden from the outside world, that
allow our object to do its business
Methods, visible to the outside world, that allow the
outside world to interact with our object
In design, there are no right or wrong solutions
But each solution usually offers its own advantages
and disadvantages

117
7.5 Object design

An Account has a balance


"has-a" is code for "needs an instance variable"
The instance variable holds the property
It is usually initialized in the constructor
Based on the user story
An Account has an initial balance
When any Account object is constructed, it must
have some initial balance

118
7.5 Object design

I need an Account public class Account {!

object

}!
119
7.5 Object design

I need an Account public class Account {!

object
!private int balance;!
An account has-a
balance

}!
120
7.5 Object design

I need an Account public class Account {!

object
!private int balance;!
An account has-a
balance

What type should we use for this


instance variable?
The story isn't clear
So I picked int, but we could
change this later if necessary

}!
121
7.5 Object design

I need an Account public class Account {!

object
!private int balance;!
An account has-a
balance !public Account(int b) {!
this.balance = b;!
Accounts are created
}!
with a specified
initial balance

}!
122
7.5 Object design

I need an Account public class Account {!

object
!private int balance;!
An account has-a
balance !public Account(int b) {!
this.balance = b;!
Accounts are created
}!
with a specified
initial balance !public Account() {!
Or, not! Assume 0 this.balance = 0;!
}!

}!
123
7.5 Object design

I Aneed
constructor can defer to another
an Account constructor
public class Account {!
using this(..);
object
!private int balance;!
An account has-a
balance !public Account(int b) {!
this.balance = b;!
Accounts are created
}!
with a specified
initial balance !public Account() {!
Or, not! Assume 0 this(0);!
}!

}!
124
7.5 Object design

I need an Account public class Account {!

object
!private int balance;!
An account has-a
balance !public Account(int b) {!
this.balance = b;!

Accounts are created }!

with a specified !public Account() {!


this(0);!

initial balance }!

Or, not! Assume 0 public int getBalance() {!


You can find out the ! !return this.balance;!
}!
balance of an account
}!

125
7.5 Object design

I need an Account public class Account {!


object
!private int balance;!
An account has-a
balance public Account(int b) {!

Accounts are created }!


this.balance = b;!

with a specified public Account() {!


initial balance }!
this(0);!

Or, not! Assume 0 public int getBalance() {!


! !return this.balance;!
You can find out the } !
balance of an account public void deposit(int f) {!
You can deposit this.balance += f;!
funds }!
}!

126
7.5 Object design

Account

Add instance variables as


needed (has-a)
private int x, y;!

127
7.5 Object design

Account

Add instance variables as


needed (has-a)
private int x, y;!
double a, b, c;!

128
7.5 Object design

getBalance()!

deposit(int funds)! Account

Add instance variables as


needed (has-a)
Add methods to provide desired
functionality!

129
7.5 Object design

getBalance()!

deposit(int funds)! Account

boolean withdraw(int c)!

Add instance variables as


needed (has-a)
Add methods to provide desired
functionality!

130
7.5 Object design
transfer(Accout a, int funds)!

getBalance()!

deposit(int funds)! Account

boolean withdraw(int c)!

Add instance variables as


needed (has-a)
Add methods to provide desired
functionality!

131 End of Oyster


BLT public 6
7.6 Exercise

Video intro
Question card
Come up with an object of interest similar to Account
Think about
Its instance variables
The functionality it offers

132
7.6 Exercise

Video response
None we will see suitable responses in the
roundtable that comes next
Roundtable with a student discussing the design

133 End of BLT


Roundtable
7.6b Explanation of Object Design

Take one object design and go over it with a


student, interactively, exploring other design
choices

134 End of Roundtable


Roundtable 7
7.7 Scope and instance variables

Go over scope with a student


Instance variables
Local variables
When are we talking about which?
Using this to mean instance variables
Using eclipse to find the one we mean

135 End of Roundtable


Monolog 8
7.8 Conclusion
Object
Collect related behaviors
Model nouns, concepts of interest
Components
Instance variables
Typically private, accessed only within a class
Methods
Typically public, offer functionality to outside world
Syntax for object usage
object.doSomething(parameters)!
As an exercise, regard the world in terms of objects
door.open()!
person.sendText("hey")!

136 End of Monologue

S-ar putea să vă placă și