Documente Academic
Documente Profesional
Documente Cultură
CHAPTER 8
OBJECTS AND
CLASSES
Chapter Goals
To understand the concepts of classes, objects
and encapsulation
To implement instance variables, methods and
constructors
To be able to design, implement, and test your
own classes
To understand the behavior of object references,
static variables and static methods
In this chapter, you will learn how to discover,
specify, and implement your own classes, and
how to use them in your programs.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 2
1
18-10-2012
Contents
Object-Oriented Programming
Implementing a Simple Class
Specifying the Public Interface of a Class
Designing the Data Representation
Implementing Instance Methods
Constructors
Testing a Class
Patterns for Object Data
Object References
Static Variables and Methods
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 3
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 4
2
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 5
Diagram of a Class
Private Data Class
Each object has its own private Private Data
data that other objects cannot (Variables)
directly access
Methods of the public interface
provide access to private data, Public Interface
while hiding implementation (Methods)
details:
This is called Encapsulation
Public Interface
Each object has a set of
methods available for other
objects to use
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 6
3
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 7
4
18-10-2012
Instantiating Objects
Objects are created based on classes
Use the new operator to construct objects
Give each object a unique name (like variables)
You have used the new operator before:
Scanner in = new Scanner(System.in);
Creating two instances of Counter objects:
Class name Object name Class name
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 9
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 10
5
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 11
6
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 14
7
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 15
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 16
8
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 17
9
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 19
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 20
10
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 21
8.6 Constructors
A constructor initializes instance variables of an
object
It is automatically called when an object is created
It has exactly the same name as the class
public class CashRegister
{
. . .
/**
Constructs a cash register with cleared item count and total.
*/
public CashRegister() // A constructor
{
itemCount = 0; Constructors never return values, but
totalPrice = 0; do not use void in their declaration
}
}
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 22
11
18-10-2012
Multiple Constructors
A class can have more than one constructor
Each must have a unique set of parameters
public class BankAccount
{ The compiler picks the constructor that
. . . matches the construction parameters.
/**
Constructs a bank account with a zero balance.
*/
public BankAccount( ) { . . . }
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance) { . . . }
}
BankAccount joesAccount = new BankAccount();
BankAccount lisasAccount = new BankAccount(499.95);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 23
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 24
12
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 25
CashRegister.java
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 26
13
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 28
14
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 29
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 30
15
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 31
CashRegisterTester.java
Test all methods
Print expected
results
Output actual results
Compare results
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 32
16
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 33
Copyright © 2011 by John Wiley & Sons. All rights reserved. Page 34
17
18-10-2012
variables
Keeping a Total
Counting Events
Collecting Values
Managing Object Properties
Modeling Objects with Distinct States
Describing the Position of an Object
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 35
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 36
18
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 38
19
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 39
20
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 41
Object Reference
21
18-10-2012
Shared References
Multiple object variables may contain references
to the same object.
Single Reference
CashRegister reg1 = new CashRegister;
Shared References
CashRegister reg2 = reg1;
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 43
22
18-10-2012
if (middleInitial == null)
System.out.println(firstName + " " + lastName);
else
System.out.println(firstName + " " + middleInitial + ". "
+ lastName);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 45
23
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 47
public BankAccount()
{
lastAssignedNumber++;
accountNumber = lastAssignedNumber;
}
. . . Methods of any object of the class can use
} or change the value of a static variable
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 48
24
18-10-2012
Example:
Each time a new account is created,
the lastAssignedNumber variable is
incremented by the constructor
Access the static variable using:
ClassName.variableName
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 49
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 50
25
18-10-2012
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 52
26
18-10-2012
27
18-10-2012
28