Java Reference
In-Depth Information
3.6
Class diagrams versus object diagrams
The structure described in the previous section (one ClockDisplay object holding two
NumberDisplay objects) can be visualized in an object diagram as shown in Figure 3.3a.
In this diagram, you see that we are dealing with three objects. Figure 3.3b shows the class
diagram for the same situation.
Figure 3.3
Object diagram and
class diagram for the
ClockDisplay
P\'LVSOD\
&ORFN'LVSOD\
ClockDisplay
1XPEHU'LVSOD\
11
KRXUV
PLQXWHV
NumberDisplay
Concept:
The class
diagram shows
the classes of an
application and
the relationships
between them. It
gives information
about the source
code and presents
the static view of a
program.
1XPEHU'LVSOD\
03
E
D
Note that the class diagram shows only two classes, whereas the object diagram shows three
objects. This has to do with the fact that we can create multiple objects from the same class.
Here, we create two NumberDisplay objects from the NumberDisplay class.
Concept:
These two diagrams offer different views of the same application. The class diagram shows the
static view. It depicts what we have at the time of writing the program. We have two classes,
and the arrow indicates that the class ClockDisplay makes use of the class NumberDisplay
( NumberDisplay is mentioned in the source code of ClockDisplay ). We also say that
ClockDisplay depends on NumberDisplay .
The object
diagram shows
the objects and
their relationships
at one moment
in time during the
execution of an
application. It gives
information about
objects at runtime
and presents the
dynamic view of a
program.
To start the program, we will create an object of class ClockDisplay . We will program the
clock display so that it automatically creates two NumberDisplay objects for itself. Thus, the
object diagram shows the situation at runtime (when the application is running). This is also
called the dynamic view.
The object diagram also shows another important detail: when a variable stores an object, the
object is not stored in the variable directly, but rather an object reference is stored in the vari-
able. In the diagram, the variable is shown as a white box, and the object reference is shown as
an arrow. The object referred to is stored outside the referring object, and the object reference
links the two.
Concept:
Object refer-
ences. Variables
of object types
store references
to objects.
It is very important to understand these two different diagrams and different views. BlueJ dis-
plays only the static view. You see the class diagram in its main window. In order to plan and
understand Java programs, you need to be able to construct object diagrams on paper or in your
 
 
Search WWH ::




Custom Search