20.5. Class DiagramsΒΆ
The two major types of relationships between classes are association and inheritance. The Unified Modeling Language (UML) is a graphical notation that provides a standard for depicting classes and the relationships between them. It gives software engineers a way to record both their proposed designs, as well as the design of the final product. Put another way, UML provides the notation for constructing blueprints for software, and since it is widely used in both industry and academia, it is important for you to have some familiarity with it.
UML defines notations for several different kinds of diagrams. Here, we will introduce class diagrams, one of the most common UML diagrams. Class diagrams can contain a number of different elements, but we will focus on the basics: showing classes with their instance variables and methods, and the relationships between classes.
On a UML class diagram, a class is depicted as a rectangle with three sections.
The top section contains the class name; the middle section, the instance
variables (attributes); and the bottom section, the methods (behaviors). The following diagram shows
the classes Point and LabeledPoint in an inheritance relationship where LabeledPoint
is a child (subclass) of Point
.
- x
- LabeledPoint inherits this attribute from Point.
- y
- LabeledPoint inherits this attribute from Point.
- label
- This is an attribute of a LabeledPoint object.
- distanceFromOrigin
- LabeledPoint inherits this method (behavior) from Point.
Q-1: Which of the following are attributes of the LabeledPoint
class as shown above. Select all that apply.
The class diagram below shows an association relationship between Point
and LabeledPoint
.
- x
- This is an attribute of Point.
- y
- This is an attribute of Point.
- label
- This is an attribute of a LabeledPoint object.
- point
- This is an attribute of a LabeledPoint object.
Q-2: Which of the following are attributes of the LabeledPoint
class as shown above. Select all that apply.
-
Q-3: Drag each item to its location in a class diagram
Read the chapter on functions and try again.
- methods
- in the bottom section of a class diagram
- attributes
- in the middle section of a class diagram
- class name
- in the top section of a class diagram