The class symbols also may be used on class and communication diagrams. Figure 1 below illustrates the class diagram for a student viewing personal and course information on Web pages. Each class has attributes and methods (which are not shown on diagrams using this notation).
If the class is a control class, the attributes would be those needed to implement the class, such as variables used just in the control class. The methods would be those used to perform calculations, make decisions, and send messages to other classes.
If the class is an entity class, the attributes represent those stored for the entity and the methods working directly with the entity, such as creating a new instance, modifying, deleting, obtaining, or printing.
Another way to enhance class diagrams is to show relationships. Relationships are connections between classes, similar to those found on an entity-relationship diagram. These are shown as lines connecting classes on a class diagram. There are two categories of relationships: associations and whole/part relationships.
The simplest type of relationship is an association, or a structural connection between classes or objects. Associations are shown as a simple line on a class diagram. The end points of the line are labeled with a symbol indicating the multiplicity, which is the same as cardinality on an entity-relationship diagram. A zero represents none, a one represents one and only one, and an asterisk represents many. The notation 0..1 represents from zero to one, and the notation 1..* represents from one to many. Associations are illustrated in Figure 2.
Class diagrams do not restrict the lower limit for an association. For example, an association might be 5..*, indicating that a minimum of five must be present. The same is true for upper limits. For example, the number of courses a student is currently enrolled in may be 1..10, representing from 1 to 10 courses. It can also include a range of values separated by commas, such as 2, 3, 4. In the UML model, associations are usually labeled with a descriptive name.
Association classes are those that are used to break up a many-to-many association between classes. These are similar to associative entities on an entity-relationship diagram. Student and Course have a many-to-many relationship, which is resolved by adding an association class called Section between the classes of Student and Course. Figure 3 illustrates an association class called Section, shown with a dotted line connected to the many-to-many relationship line.
An object in a class may have a relationship to other objects in the same class, called a reflexive association. An example would be a task having a precedent task, or an employee supervising another employee. This is shown as an association line connecting the class to itself, with labels indicating the role names, such as task and precedent task.
Whole/part relationships are when one class represents the whole object and other classes represent parts. The whole acts as a container for the parts. These relationships are shown on a class diagram by a line with a diamond on one end. The diamond is connected to the object that is the whole. Whole/part relationships (as well as aggregation, discussed later) are shown in Figure 4.
A whole/part relationship may be an entity object that has distinct parts, such as a computer system that includes the computer, printer, display, and so on, or an automobile that has an engine, brake system, transmission, and so on. Whole/part relationships may also be used to describe a user interface, in which one GUI screen contains a series of objects such as lists, boxes, or radio buttons, or perhaps a header, body, and footer area. Whole/part relationships have three categories: aggregation, collection, and composition.
Aggregation An aggregation is often described as a “has a” relationship. Aggregation provides a means of showing that the whole object is composed of the sum of its parts (other objects). In the student enrollment example, the department has a course and the course is for a department. This is a weaker relationship, because a department may be changed or removed and the course may still exist. A computer package may not be available any longer, but the printers and other components still exist. The diamond at the end of the relationship line is not filled in.
Collection A collection consists of a whole and its members. This may be a voting district with voters or a library with books. The voters or books may change, but the whole retains its identity. This is a weak association.
Composition Composition, a whole/part relationship in which the whole has a responsibility for the part, is a stronger relationship, and is usually shown with a filled-in diamond. Keywords for composition are one class “always contains” another class. If the whole is deleted, all parts are deleted. An example would be an insurance policy with riders. If the policy is canceled, the insurance riders are also canceled. In a database, the referential integrity would be set to delete cascading child records. In a university there is a composition relationship between a course and an assignment as well as between a course and an exam. If the course is deleted, assignments and exams are deleted as well.