Java Reference
In-Depth Information
10.1 Introduction
The focus of this chapter is on class design and explores the differences between
procedural programming and object-oriented programming.
Key
Point
The preceding chapter introduced objects and classes. You learned how to define classes,
create objects, and use objects from several classes in the Java API (e.g., Circle , Date ,
Random , and Point2D ). This topics approach is to teach problem solving and fundamental
programming techniques before object-oriented programming. This chapter shows how pro-
cedural and object-oriented programming differ. You will see the benefits of object-oriented
programming and learn to use it effectively.
Our focus here is on class design. We will use several examples to illustrate the advantages
of the object-oriented approach. The examples involve designing new classes and using them
in applications and introducing new classes in the Java API.
10.2 Class Abstraction and Encapsulation
Class abstraction is the separation of class implementation from the use of a class.
The details of implementation are encapsulated and hidden from the user. This is
known as class encapsulation.
Key
Point
In Chapter 6, you learned about method abstraction and used it in stepwise refinement. Java
provides many levels of abstraction, and class abstraction separates class implementation
from how the class is used. The creator of a class describes the functions of the class and lets
the user know how the class can be used. The collection of methods and fields that are accessi-
ble from outside the class, together with the description of how these members are expected to
behave, serves as the class's contract . As shown in Figure 10.1, the user of the class does not
need to know how the class is implemented. The details of implementation are encapsulated
and hidden from the user. This is called class encapsulation . For example, you can create a
Circle object and find the area of the circle without knowing how the area is computed. For
this reason, a class is also known as an abstract data type (ADT).
class abstraction
class's contract
class encapsulation
abstract data type
Class implementation
is like a black box
hidden from the clients
Class Contract
(signatures of
public methods and
public constants)
Clients use the
class through the
contract of the class
Class
F IGURE 10.1
Class abstraction separates class implementation from the use of the class.
Class abstraction and encapsulation are two sides of the same coin. Many real-life exam-
ples illustrate the concept of class abstraction. Consider, for instance, building a computer
system. Your personal computer has many components—a CPU, memory, disk, motherboard,
fan, and so on. Each component can be viewed as an object that has properties and methods.
To get the components to work together, you need know only how each component is used
and how it interacts with the others. You don't need to know how the components work
internally. The internal implementation is encapsulated and hidden from you. You can build a
computer without knowing how a component is implemented.
The computer-system analogy precisely mirrors the object-oriented approach. Each com-
ponent can be viewed as an object of the class for the component. For example, you might
have a class that models all kinds of fans for use in a computer, with properties such as fan
size and speed and methods such as start and stop. A specific fan is an instance of this class
with specific property values.
As another example, consider getting a loan. A specific loan can be viewed as an object
of a Loan class. The interest rate, loan amount, and loan period are its data properties, and
 
 
 
 
Search WWH ::




Custom Search