Relations among different classes might be clear for me, however, the way using unified modeling language (UML) to represent it is something new to me. A note on this topic is worth taking to make it on my dictionary. Taken from Core Java Volume I (11th edition) - Cay S. Horstmann.
Relations between classes
The most common relationships between classes are:
- Dependence (“uses-a”)
- Aggregation (“has-a”)
- Inheritance (“is-a)
The dependence, or “uses-a” relationship, is the most obvious and also the most general. For example, the
Order class uses the
Account class because
Order objects need to acess
Accout objects to check for credit status. But the
Item class does not depend on the
Account class, because
Item objects never need to worry about customer accounts. Thus, a class depends on another class if its methods use or manipulate objects of that class.
The aggregation, or “has-a” relationship, is easy to understand because it is concrete; for example, an
Order object contains
Item objects. Containment means that objects of class A contain objects of class B.
The inheritance, or “is-a” relationship, expresses a relationship between a more special and a more general class. For example, a
RushOrder class inherits from an
Order class. The specialized
RushOrder class has special methods for priority handling and a different method for computing shipping charges, but its other methods, such as adding items and billing, are inherited from the
Order class. In general, if class A extends class B, class A inherits methods from class B but has more capabilities.
Unified modeling language
Many programmers use the UML (Unified Modeling Language) notation to draw class diagrams that describe the relationships between classes. An example of such diagram as below:
You draw classes as rectangles, and relationships as arrows with various adornments. Following table shows the most common UML arraow styles.
For more information on UML notation, please refer to this brief summary.