A First Look at Classes: Starting Out With Java: From Control Structures Through Objects Fifth Edition
A First Look at Classes: Starting Out With Java: From Control Structures Through Objects Fifth Edition
Chapter Topics
Chapter 6 discusses the following main topics:
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-2
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-3
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Scanner
object
Random
object
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
PrintWriter
object
See ObjectDemo.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-11
6-12
UML Diagram
Unified Modeling Language (UML) provides a
set of standard diagrams for graphically
depicting object-oriented systems.
Class name goes here
Fields are listed here
Methods are listed here
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-13
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-14
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-15
Access Specifiers
An access specifier is a Java keyword that indicates
how a field or method can be accessed.
public
When the public access specifier is applied to a class
member, the member can be accessed by code inside the
class or outside.
private
When the private access specifier is applied to a class
member, the member cannot be accessed by code outside the
class. The member can be accessed only by methods that
are members of the same class.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-16
Access
specifier
Return
Type
Method
Name
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-17
6-18
A Rectangle object
address
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
length:
0.0
width:
0.0
6-19
A Rectangle object
address
length: 10.0
width: 0.0
6-20
6-21
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-22
6-23
setWidth
getWidth
6-24
Data Hiding
An object hides its internal, private fields from
code that is outside the class that the object is
an instance of.
Only the class's methods may directly access
and make changes to the objects internal data.
Code outside the class must use the class's
public methods to operate on an object's private
fields.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Data Hiding
Data hiding is important because classes are
typically used as components in large software
systems, involving a team of programmers.
Data hiding helps enforce the integrity of an
object's internal data.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Stale Data
Some data is the result of a calculation.
Consider the area of a rectangle.
length width
6-27
Stale Data
Rather than use an area variable in a Rectangle
class:
public double getArea()
{
return length * width;
}
6-28
Rectangle
- width : double
+ setWidth(w : double) : void
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-29
- width : double
+ setWidth(w : double) : void
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-30
- width : double
+ setWidth(w : double) : void
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-31
Rectangle
- width : double
+ setWidth(w : double) : void
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-32
ClassName
Fields
Methods
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-33
Rectangle
- width : double
- length : double
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-34
Rectangle
- width : double
- length : double
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-35
6-36
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-37
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-38
address
length: 10.0
width: 14.0
address
length: 15.0
width: 12.0
address
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
length: 20.0
width: 30.0
6-39
Constructors
Classes can have special methods called constructors.
A constructor is a method that is automatically called
when an object is created.
Constructors are used to perform operations at the time
an object is created.
Constructors typically initialize instance fields and
perform other object initialization tasks.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-40
Constructors
Constructors have a few special properties that
set them apart from normal methods.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-41
6-42
Constructors in UML
In UML, the most common way constructors
are defined is:
Rectangle
- width : double
- length : double
Notice there is no
return type listed
for constructors.
+Rectangle(len:double, w:double)
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-43
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-44
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-45
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-46
6-47
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-48
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-49
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-51
6-52
Signatures of the
add methods of
previous slide
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-53
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-54
The first call would use the no-arg constructor and box1 would
have a length of 1.0 and width of 1.0.
The second call would use the original constructor and box2
would have a length of 5.0 and a width of 10.0.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-55
BankAccount
-balance:double
+BankAccount()
Overloaded Constructors
+BankAccount(startBalance:double)
+BankAccount(strString):
+deposit(amount:double):void
+deposit(str:String):void
+withdraw(amount:double):void
+withdraw(str:String):void
+setBalance(b:double):void
+setBalance(str:String):void
+getBalance():double
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-56
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-57
Shadowing
A parameter variable is, in effect, a local variable.
Within a method, variable names must be unique.
A method may have a local variable with the same name as
an instance field.
This is called shadowing.
The local variable will hide the value of the instance field.
Shadowing is discouraged and local variable names should
not be the same as instance field names.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-58
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-59
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-60
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-61
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6-62