Class Diagram Tutorial (UML)
What is a Class Diagram?
A Class Diagram is a type of static structure diagram in UML that describes the structure of a
system by showing:
• Classes
• Attributes (data members)
• Methods (functions or operations)
• Relationships between classes
Purpose of a Class Diagram
• To visualize the blueprint of a system.
• To show object-oriented design clearly.
• Helps in code generation and reverse engineering.
• Useful in database modeling.
Components of a Class Diagram
Component Description
Class Represents a blueprint of objects.
Attributes The data/fields a class holds.
Methods The behaviors/functions of the class.
Relationships How classes are connected (association, inheritance, etc.).
Class Box Notation
+------------------------+
| Class Name |
+------------------------+
| - attribute1: Type |
| - attribute2: Type |
+------------------------+
| + method1(): ReturnType|
| + method2(arg): void |
+------------------------+
Symbols:
• + Public
• - Private
• # Protected
Example Class: Student
+----------------------+
| Student |
+----------------------+
| - name: String |
| - age: int |
+----------------------+
| + enroll(): void |
| + getDetails(): String |
+----------------------+
Relationships in Class Diagrams
Relationship Symbol/Notation Description
Association Line Basic connection
Inheritance (Generalization) Line with triangle One class is a subclass of another
Aggregation Line with white diamond "Has-a" relationship (weak)
Composition Line with black diamond "Owns-a" relationship (strong)
Dependency Dotted line with arrow Uses temporarily
Example: E-Commerce System
Classes:
• Customer
• Order
• Product
Relationships:
• A Customer places an Order
• An Order contains multiple Products
Text Representation
Customer
---------
- name: String
- email: String
+ placeOrder()
Order
---------
- orderId: int
- date: Date
+ addProduct()
Product
---------
- productId: int
- name: String
+ getPrice()
Relationships:
• Customer → Order: Association
• Order → Product: Composition (Order owns Product details at that time)
Class Diagram
• .
Classes in the Diagram
1. Customer
• Attributes:
o name: String
o email: String
• This class represents the users who place orders on the system.
2. Order
• Attributes:
o orderId: int
o date: Date
• Represents a customer's purchase order.
• An order is made by a customer and may include multiple products.
3. Product
• Attributes:
o productID: int
o name: String
• Represents a product that can be included in one or more orders.
Relationships
Association & Multiplicity
• Customer → Order
There is a line connecting Customer to Order with a diamond and a *:
o This shows a one-to-many relationship.
o One customer can place many orders.
o The diamond indicates aggregation (a customer "has" orders, but orders can exist
independently).
• Order → Product
o There's also a line from Order to Product.
o Suggests that an order contains multiple products.
o This could also be an aggregation (or even composition, if products are
considered part of an order).
Summary in Simple Terms
• A Customer can place many Orders.
• An Order contains many Products.
• The diagram models the real-world structure of an online store's database.
Code
[Link]
public class Product {
private int productID;
private String name;
public Product(int productID, String name) {
[Link] = productID;
[Link] = name;
// Getters and Setters
public int getProductID() {
return productID;
public void setProductID(int productID) {
[Link] = productID;
public String getName() {
return name;
public void setName(String name) {
[Link] = name;
}
[Link]
import [Link];
import [Link];
public class Order {
private int orderId;
private Date date;
private List<Product> products;
public Order(int orderId, Date date, List<Product> products) {
[Link] = orderId;
[Link] = date;
[Link] = products;
// Getters and Setters
public int getOrderId() {
return orderId;
public void setOrderId(int orderId) {
[Link] = orderId;
public Date getDate() {
return date;
public void setDate(Date date) {
[Link] = date;
public List<Product> getProducts() {
return products;
public void setProducts(List<Product> products) {
[Link] = products;
public void addProduct(Product product) {
[Link](product);
[Link]
java
CopyEdit
import [Link];
public class Customer {
private String name;
private String email;
private List<Order> orders;
public Customer(String name, String email, List<Order> orders) {
[Link] = name;
[Link] = email;
[Link] = orders;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
[Link] = orders;
}
public void placeOrder(Order order) {
[Link](order);
}
}
🛠 Optional: [Link] to test the setup
java
CopyEdit
import [Link].*;
public class Main {
public static void main(String[] args) {
Product p1 = new Product(101, "Laptop");
Product p2 = new Product(102, "Mouse");
List<Product> productList = new ArrayList<>();
[Link](p1);
[Link](p2);
Order order = new Order(1, new Date(), productList);
List<Order> orderList = new ArrayList<>();
[Link](order);
Customer customer = new Customer("Alice", "alice@[Link]",
orderList);
[Link]("Customer: " + [Link]());
[Link]("Orders: " + [Link]().size());
}
}