A Second Look at Classes and Objects: Starting Out With Java: From Control Structures Through Objects Fifth Edition
A Second Look at Classes and Objects: Starting Out With Java: From Control Structures Through Objects Fifth Edition
Chapter Topics
Chapter 8 discusses the following main topics:
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-2
Chapter Topics
Chapter 8 discusses the following main topics:
Aggregation
The this Reference Variable
Enumerated Types
Garbage Collection
Focus on Object-Oriented Design: Class
Collaboration
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-3
8-4
Class name
Static method
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-5
Static Fields
Class fields are declared using the static keyword
between the access specifier and the field type.
private static int instanceCount = 0;
8-6
Static Fields
instanceCount field
(static)
3
Object1
Object2
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Object3
8-7
Static Methods
Methods can also be declared static by placing the static
keyword between the access modifier and the return type of
the method.
public static double milesToKilometers(double miles)
{}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-8
Static Methods
Static methods are convenient because they may be
called at the class level.
They are typically used to create utility classes, such as
the Math class in the Java Standard Library.
Static methods may not communicate with instance
fields, only static fields.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-9
8-10
A Rectangle object
displayRectangle(box);
length: 12.0
width: 5.0
Address
public static void displayRectangle(Rectangle r)
{
// Display the length and width.
System.out.println("Length: " + r.getLength() +
" Width: " + r.getWidth());
}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-11
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-12
balance:
3200.0
address
8-13
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-14
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-15
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-16
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-17
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-18
8-19
Example: ObjectCopy.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-20
Copy Constructors
A copy constructor accepts an existing object of the same class
and clones it
public Stock(Stock object 2)
{
symbol = object2.symbol;
sharePrice = object2.sharePrice;
}
// Create a Stock object
Stock company1 = new Stock("XYZ", 9.62);
//Create company2, a copy of company1
Stock company2 = new Stock(company1);
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-21
Aggregation
Creating an instance of one class as a reference in
another class is called object aggregation.
Aggregation creates a has a relationship between
objects.
Examples:
Instructor.java, Textbook.java, Course.java,
CourseDemo.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-22
- Instructor : Instructor
- textBook : TextBook
TextBook
Instructor
- lastName : String
- firstName : String
- officeNumber : String
+ Instructor(lname : String, fname : String,
office : String)
+Instructor(object2 : Instructor)
+set(lname : String, fname : String,
office : String): void
+ toString() : String
- title : String
- author : String
- publisher : String
+ TextBook(title : String, author : String, publisher :
String)
+ TextBook(object2 : TextBook)
+ set(title : String, author : String, publisher : String)
: void
+ toString() : String
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-23
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-24
Null References
A null reference is a reference variable that points to nothing.
If a reference is null, then no operations can be performed on it.
References can be tested to see if they point to null prior to
being used.
if(name != null)
{
System.out.println("Name is: "
+ name.toUpperCase());
}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-25
8-26
8-27
Enumerated Types
Known as an enum, requires declaration and
definition like a class
Syntax:
enum typeName { one or more enum constants }
Definition:
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY }
Declaration:
Day WorkDay; // creates a Day enum
Assignment:
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-28
Enumerated Types
An enum is a specialized
class
Each are objects of type Day, a specialized class
Day.SUNDAY
Day workDay = Day.WEDNESDAY;
Day.MONDAY
Day.TUESDAY
address
Day.WEDNESDAY
Day.THURSDAY
Day.FRIDAY
Day.SATURDAY
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-29
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-30
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-31
Garbage Collection
When objects are no longer needed they should be
destroyed.
This frees up the memory that they consumed.
Java handles all of the memory operations for you.
Simply set the reference to null and Java will reclaim
the memory.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-32
Garbage Collection
The Java Virtual Machine has a process that runs in the
background that reclaims memory from released objects.
The garbage collector will reclaim memory from any object
that no longer has a valid reference pointing to it.
BankAccount account1 = new BankAccount(500.0);
BankAccount account2 = account1;
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-33
Garbage Collection
A BankAccount object
account1
Address
account2
Address
Balance:
500.0
8-34
Garbage Collection
A BankAccount object
account1
null
account2
Address
Balance:
500.0
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-35
Garbage Collection
A BankAccount object
account1
null
account2
null
Balance:
500.0
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-36
Garbage Collection
A BankAccount object
account1
null
account2
null
Balance:
500.0
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-37
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-38
Class Collaboration
Collaboration two classes interact with each other
If an object is to collaborate with another object, it
must know something about the second objects
methods and how to call them
If we design a class StockPurchase that
collaborates with the Stock class (previously
defined), we define it to create and manipulate a
Stock object
See examples: StockPurchase.java, StockTrader.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8-39
CRC Cards
Class, Responsibilities and Collaborations (CRC) cards are
useful for determining and documenting a classs
responsibilities
The things a class is responsible for knowing
The actions a class is responsible for doing
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Stock class
None
Stock class
None or class name
8-40