UML Class Diagrams and Accessibility
Estimated time to read: 3 minutes
Overview¶
CRC cards help to prototype the relationships between objects and their responsibilities whilst UML Cards enable us to create Class Diagrams. From the a Class Diagram, it is easy to develop code.
UML Cards¶
UML Cards contain the following information:
Class Name¶
This is the name of the class. In Java, an object is an instance of a class.
Properties¶
Attributes within a class are known as 'fields' in Java.
Operations¶
Operations are the 'methods' that the class contains.
// <name> ( <parameter list > ) : <return type>
isOnSale(): boolean;
is OnSale( date: Date): boolean; // If date matches Date, then return true
UML Card Layout¶
Access Modifiers¶
Access modifiers change which classes are able to access attributes and behaviours. They also determine which attribute and behaviours a superclass will share with its subclasses.
Modifier | Sign | Definition |
---|---|---|
Public | + | A public attribute can be accessed from anywhere. |
Private | - | A private attribute is only visible from within the class. It can not be accessed from outside the class. |
Protected | # | In Java, a protected attribute or method can only be accessed by: - The encapsulating class itself - All subclasses - All classes within the same package |
Package | ~ | A package is a collection of packages that exist in the same namespace |
Derived | / | A derived attribute is produced or calculated from other properties. It is common to mark it as read-only. |
Class / Static | underlined | The static modifier makes an attribute or method of a class independent. This means that the value does not change across instances of the class. |
Access Accessibility Matrix¶
Modifier | Class | Package | Subclass | Other Classes |
---|---|---|---|---|
Private | Yes | No | No | No |
No Modifier | Yes | Yes | No | No |
Protected | Yes | Yes | Yes | No |
Public | Yes | Yes | Yes | Yes |
UML Relationships¶
Association: A solid line between two classes. Add arrows at either end or both ends to show that the classes are aware (or unaware) of each other.
Dependency: A dashed line with an open arrow. If the definition of class 2 changes, it will change class 1, but not the other way around (depends on).
Aggregation: An open diamond at class 1. A special type of association that shows that class 2 is a part of class 1.
Composition: A solid diamond at class 1. A special type of aggregation that shows that class 2 can not exist without class 1.
Inheritance (generalisation): An open triangle at the parent class. This shows that the subclasses are specialisations of the parent class - they extend the parent class.
Implementation (realisation or execution): A dashed line with an open triangle at the blueprint class. This is used where a class implements the functionality of a ‘blueprint’ class, and may be implemented differently depending on each class that implements it.