Object Oriented Programming and Data Abstraction
Object Diagrams
Java programmers need to understand the difference between primitive types
and object types. For this reason, it is helpful to draw object diagrams.
The primitive types are int, boolean, float, double, char, ... (they always
begin with a lower case letter).
The object types are classes (and interfaces) which have been defined by
java programmers. Some common object types are String, List, ArrayList, HashSet, ...
(they begin with upper case letters).
When drawing an object diagram for a variable (whether it is a local variable,
a parameter, or a field):
- Draw a box, and label the box with the variable's name.
- Put the value of the variable inside the box:
- If the variable has not been assigned a value, use the following:
- If it is an int, float, double, the value is 0
- It it is a boolean, the value is false
- If it is a char, the value is a space
- If it is an object type, the value is null.
- If the variable is an object type which has been assigned
a value, that value will be a reference
to an object. Show this with an arrow to a large box in which the
object's data is shown.
- In the object's data box, show the name of each field (i.e.
instance variable), and its value in a box,
using these instructions.
- For objects which are collections, use these guidelines:
- Lists, ArrayLists, LinkedLists: Show the size of the
list as a variable. Show each element of the list with
an index value (0,1,2...) labeling the references to the
items in the list.
- Sets, HashSets, TreeSets: Show the size of the set
as a variable. Show each element of the set as a box
storing a reference to its value (no index numbers for sets).
- Maps, HashMaps, TreeMaps: Show the size of the map (i.e.
the number of entries). Show the entries in two columns, one
column labeld keys and the other column labeled
values (no index numbers for the entries).
- Continue to apply these instructions until all relevant
variables have values.
- To simplify object diagrams, it is ok to treat Strings and
wrapper classes (i.e. Integer, Boolean, Character, ...) as primitives.
An example of an object diagram.