4.6 Abstraction | 179
(^3) The Java library includes a Dateclass,java.util.Date. However, the familiar properties of dates make them
a natural example to use in explaining the concepts of class interface design. We ignore the existence
of the library class in the discussion that follows, as if we must design our own Dateclass. In writing real
programs, however, you would probably use the GregorianCalendarlibrary class that replaced Datein
Java 1.2.
It is important to understand that encapsulation isn’t a Java language construct. We
achieve encapsulation by carefully designing the class interface. Once that task is complete,
we take advantage of Java features that simplify implementation of an encapsulated class.
4.6 Abstraction
How do we design a class with an encapsulated implementation? We begin with an
abstraction for the real-world object. That way, we can focus on the class interface inde-
pendently of how we may implement it. As a result, the implementation is automatically sep-
arated from the interface. We therefore begin with a discussion of abstraction in programming.
Data and Control Abstraction
Creation of a new class begins when we need a new kind of object. We create a list of the
responsibilities associated with the class. (In Chapter 6, we will formalize a process that
helps us do this; for now, however, we will just work with classes in which it is easy to identify
some obvious responsibilities.) Some responsibilities require us to supply data to the class
(for example, constructors or voidmethods), while others return data (value-returning meth-
ods such as readLine). Each responsibility has a control component (what it does) and a data
component (the information it needs or returns). Encapsulation allows us to create an
abstraction of either or both of these components.
Data abstractionis the separation of the external representation of an object’s
values from their internal implementation. For example, the external represen-
tation of a date might be integer values for the day and year, and a string that spec-
ifies the name of the month. But we might implement the date within the class
using a standard value that calendar makers call the Julian day, which is the
number of days since January 1, 4713 BC.
The advantage of using the Julian day is that it simplifies arithmetic on dates,
such as computing the number of days between dates. All of the complexity of deal-
ing with leap years and the different number of days in the months is captured in
formulas that convert between the conventional representation of a date and the
Julian day. From the user’s perspective, however, the methods of aDateobject receive
and return a date as two integers and a string.^3
In many cases, the external representation and the implementation of the values are
identical. However, we won’t tell that to the user, in case we decide to change the imple-
mentation in the future.
Control abstractionis the separation of the logical properties of the actions of a responsi-
bility from their implementation. For example, the documentation for the Dateclass says that
it takes into account all of the special leap-year rules. In the implementation, only the Julian
Data abstraction The separa-
tion of the logical representa-
tion of an object’s range of
values from their implementa-
tion
Control abstraction The sepa-
ration of the logical properties
of the operations on an object
from their implementation