Documente Academic
Documente Profesional
Documente Cultură
*
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
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
}!
5
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations (names)
Methods
! !private int balance;! Braces for scope
}!
6
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations
Methods
! !private int balance;! Braces for scope
}!
7
7.1 Overview of an object
We see familiar elements
public class Account {!
Declarations
Methods
! !private int balance;! Braces for scope
}!
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
}!
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
}!
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
}!
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
}!
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
}!
13
7.1 Overview of an object
public class Account {!
}!
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 {!
}!
16
7.1 Overview of an object
public class Account {!
}!
21
7.1 Overview of an object
public class Account {!
}!
22
7.1 Overview of an object
public class Account {!
! !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!
}!
27
7.1 Overview of an object
public class Account {!
! !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 {!
! !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 {!
}!
31
7.1 Overview of an object
public class Account {! Recall
Constructor is obligated to
! !private int balance;! initialize the instance
variables
}!
32
7.1 Overview of an object
public class Account {! Recall
Constructor is obligated to
! !private int balance;! initialize the instance
variables
}!
33
7.1 Overview of an object
public class Account {! new Account(100)!
}!
34
7.1 Overview of an object
public class Account {! new Account(100)!
}!
35
7.1 Overview of an object
public class Account {! new Account(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!
}!
37
7.1 Overview of an object
public class Account {!
}!
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
}!
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
}!
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
}!
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!
}!
42
7.1 Overview of an object
How do you find the
public class Account {!
instance variables of an
object?
! !private int balance;!
}!
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;!
}!
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 {!
}!
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)
}!
60
7.1 Overview of an object
public class Account {! Class name
}!
61
7.1 Overview of an object
public class Account {! Instance variables
}!
62
7.1 Overview of an object
public class Account {! Constructors
}!
63
7.1 Overview of an object
public class Account {! Methods
}!
64 End of Oyster
BLT public 2
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);!
68
Oyster 3
7.3 How to make and use objects
Account acct1 = new Account(100);!
69
7.3 How to make and use objects
Account acct1 = new Account(100);!
75
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
76
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
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);!
81
7.3 How to make and use objects
Account acct1 = new Account(100);!
100
Account acct2 = new Account(50);!
acct1.deposit(35);!
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);!
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());!
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());!
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());!
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());!
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());!
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());!
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());!
99 End of Oyster
Roundtable
7.3b Roundtable
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
105
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap
acct1! 100
348
106
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap
acct1! 100
348
348
107
7.4 How are objects stored?
Account acct1 = new Account(100);!
Heap
Account acct2 = new Account(50);!
100
acct1!
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
50
408
public class Account {!
}!
111
7.4 How are objects stored?
Heap
}!
112
7.4 How are objects stored?
Heap
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
Heap
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
117
7.5 Object design
118
7.5 Object design
object
}!
119
7.5 Object design
object
!private int balance;!
An account has-a
balance
}!
120
7.5 Object design
object
!private int balance;!
An account has-a
balance
}!
121
7.5 Object design
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
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
object
!private int balance;!
An account has-a
balance !public Account(int b) {!
this.balance = b;!
initial balance }!
125
7.5 Object design
126
7.5 Object design
Account
127
7.5 Object design
Account
128
7.5 Object design
getBalance()!
129
7.5 Object design
getBalance()!
130
7.5 Object design
transfer(Accout a, int funds)!
getBalance()!
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