UML2 Class Diagram in Java
In the modelling world Class diagram forms the major chunk of the Unified Modelling Language (UML) architecture diagram. In this article we are planning to show some of the key usage of the class diagram and how they can be represented in Java. This article can be used as a reference for your modelling reference.
UML was developed by Grady Booch, Ivar Jacobson, Jim Rumbaugh at Rational Software in 1990. To get more history on the UML development, it’s progress and story of standardisation by OMG (please don’t confuse this with the current SMS language of Oh My God, it is Object Management Group (OMG)) you can refer to Martin Fowler’s UML Distilled book . He has explained very clearly about the practical use of the various UML diagrams in modelling.
In my article we will go straight to the point making it a crisp reference document.
Class Diagram Example:
Class Diagram Example
Car.java
public class Car {
private String carColor;
private double carPrice = 0.0;
public String getCarColor(String model) {
return carColor;
}
public double getCarPrice(String model) {
return carPrice;
}
}
The above example of Car class is self explanatory. The Car class has private instance variables carColor, carPrice denoted by (-) in the UML Class diagram. Similarly if this was public then it would have been represented as (+), if was protected then it is denoted by (#). The package visibility is defined by (~).
Java visibility | UML Notation |
---|---|
public | + |
private | - |
Protected | # |
package | ~ |
The return type of the instance variables or the methods are represented next to the colon (:) sign.
Structure:
[visibility] [multiplicity] [:type [=default value]] {property string}
Example: carPrice : double = 0.0
Representing Static variable or static operation:
The static data is represented with an underline. Let’s take the below example.
Class diagram
Code:
public class Employee {
private static String department = "R&D";
private int empId;
private Employee(int employeeId) {
this.empId = employeeId;
}
public static String getEmployee(int emplId) {
if (emplId == 1) {
return "idiotechie";
} else {
return "Employee not found";
}
}
public static String getDepartment() {
return department;
}
}
Association:
The association represents the static relationship between two classes along with the multiplicity. E.g. an employee can have one primary address associated with it but can have multiple mobile numbers.
Association are represented as thin line connecting two classes. Association can be unidirectional (shown by arrow at one end) or bidirectional (shown by arrow at both end).
Multiplicity defines how many instances can be associated at any given moment.
0..1 | No instances or one instance | A flight seat can have no or one passenger only |
1 | Exactly one instance | An order can have only one customer |
0..* or * | Zero or more instances | A class can have zero or more students. |
1..* | One or more instances (at least one) | A flight can have one or more passenger |
The unidirectional relationship shows that the source object can invoke methods of the destination class. In Java a possible example can be the instance variable of source class referencing the destination class.
Association Example
Association:
The association represents the static relationship between two classes along with the multiplicity. E.g. an employee can have one primary address associated with it but can have multiple mobile numbers.
Association are represented as thin line connecting two classes. Association can be unidirectional (shown by arrow at one end) or bidirectional (shown by arrow at both end).
Multiplicity defines how many instances can be associated at any given moment.
0..1 | No instances or one instance | A flight seat can have no or one passenger only |
1 | Exactly one instance | An order can have only one customer |
0..* or * | Zero or more instances | A class can have zero or more students. |
1..* | One or more instances (at least one) | A flight can have one or more passenger |
The unidirectional relationship shows that the source object can invoke methods of the destination class. In Java a possible example can be the instance variable of source class referencing the destination class.
Association Example
public class Customer {
private String name;
private String address;
private String contactNumber;
}
public class Car {
private String modelNumber;
private Customer owner;
}
Let’s look at an example of bidirectional association:
Bidirectional association
public class Customer {
private String name;
private String address;
private String contactNumber;
private Car car;
}
public class Car {
private String modelNumber;
private Customer owner;
}
In the bidirectional association each of the class in this relationship refers to each other by calling each others method. In the above Java example it is depicted as instance variable of Car class in called inside the Customer class and vice versa.
In the above example the car and owner refers to the roles and is depicted by the name of instance variable in the code.
Multiplicity:
Assume a scenario where a customer has multiple cars. How do we represent this situation in Java and UML?
Multiplicity in association
The above diagram explains a unidirectional association with a one to may relationship. Both use of ArrayList and Array is for illustration purposes only.
Car.java
public class Car {
private String brand;
public Car(String brands){
this.brand = brands;
}
public Car() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
Customer.java
public class Customer {
private Car[] vehicles;
ArrayList<Car> carList = new ArrayList<Car>();
public Customer(){
vehicles = new Car[2];
vehicles[0] = new Car("Audi");
vehicles[1] = new Car("Mercedes");
carList.add(new Car("BMW"));
carList.add(new Car("Chevy"));
}
}
Generalization
This property represents the inheritance feature of the object oriented concept. In Java this can relate to the “extends” keyword. The inheritance should ideally follow the Liskov Substitution Principle i.e. the subtype should be able to substitute for its supertype. It helps to make the code implicitly follow the Open Close Principle i.e. Open for extension but closed for modification.
Generalization
public class Car {
private String model;
public void printPrice() {
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
public class hatchback extends Car {
private String model;
public void printPrice() {
System.out.println("Hatchback Price");
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
Realization:
This is related to the relationship between the class and the interface. The realization is equivalent to the “implements” keyword in Java.
Realization in Java
Realization can also be represented as :
Realization – alternative
This is very straight forward implementation so hopefully there will ne no code provided. Unlike Generalization in this case the arrow is dashed.
Dependency
Dependency is a relationship that shows that a class is dependent on another class for its existence or implementation. Dependency relationship is shown as a dotted line with an arrow from source class to the dependent class.
In Java we can consider the dependency relationship if the source class has a reference to the dependent class directly or source class has methods through which the dependent objects are passed as a parameter or refers to the static operation’s of the dependent class or source class has a local variable referring to the dependent class etc.
Dependency
The above diagram satisfies dependency relationship as the source class Order passes the PaymentSystem reference through the processPayment().
public class PaymentSystem {
}
public class Order {
public void processPayment(PaymentSystem ps){
}
}
Aggregation:
This shows “has a” relationship. It is a form of association relationship. This relationship highlights that a whole is made of its parts. So if a whole is destroyed the part still remains.
In UML this is represented through a hollow diamond with the diamond symbol pointing towards the whole.
In case of Java the aggregation follows the same structure as association. It is represented through the instance variables of a class.
Aggregation
public class Student {
}
public class School {
private Student student;
}
In this case a student is a part of the School. However during design it is preferred to use association instead of aggregation as it is not a recommended option.
Composition:
This is again a whole or part relationship where if the whole is destroyed then the part cannot exist independently. Another important point about Composition is that the part at any point in time can have only one owner. E.g. A person can be an employee of one company at any point in time due to contractual obligations. That person cannot hold dual work authorisation. If the Company goes bankrupt the employee of this company does not exist and will be fired.
The composition is represented as a filled diamond with data flowing in single direction from the whole to the part.
The composition in Java is represented in the same form as aggregation with help of instance variables.
Composition
public class Employee {
}
public class Company {
private Employee[] employee;
}
Question: Can a singleton class be represented as Aggregation? |
Answer: No. The aggregation does not support cyclic relationship. However in case of Singleton it requires a cyclic relationship with itself. |
This article can be used as a reference article while coding from design in Java.