Unit 5: UML

Table of Contents

  1. UML
  2. Overall Structure
  3. Visibility Modifiers
  4. Fields
  5. Methods
  6. More UML Rules

UML

UML stands for Unified Modeling Language. UML diagrams are used by developers to repesent classes and relationships between classes. There are quite a bit of rules on how to make these diagrams, so we’ll just go over the basics.

Overall Structure

A UML diagram for a class is essentially a table with 3 rows. The 1st row contains the class name, the 2nd row contains the fields, and the 3rd row contains the methods.

Let’s start creating a UML diagram for the Human class.

public class Human {
    private String name;
    private int age;
    private String gender;

    public Human() {
        name = "Jane Doe";
        age = 18;
        gender = "female";
    }

    public Human(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public void eat(String food) {
        System.out.println(name + " ate " + food);
    }

    public void walk(double miles) {
        System.out.println(name + " walked " + miles + " miles");
    }

    public void talk(String message) {
        System.out.println(name + " said " + message);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

UML Diagram

Human
Fields will go here
Methods will go here

Visibility Modifiers

When listing fields and methods, you should use special symbols to denote the visibility modifier associated with that field/method.

Visibility Modifier UML Diagram Symbol
private -
default n/a
protected #
public +

Static fields or methods are underlined.

Fields

To list a field, you need to specify its visibility, name, and type. For example:

Human
-name: String
-age: int
-gender: String
Methods will go here

Methods

To list a method, you need to specify its visibility, name, parameter names, parameter types, and return type.

Note that for constructors, you don’t need to specify a return type. However, if a method returns void, you need to explicitly specify that in the diagram.

Human
-name: String
-age: int
-gender: String
+Human()
+Human(name: String, age: int: gender: String)
+eat(food: String): void
+walk(miles: double): void
+talk(message: String): void
+getname(): String
+setName(name: String): void
+getAge(): int
+setAge(age: int): void
+getGender(): String
+setGender(gender: String): void

More UML Rules

If you want to learn more rules regarding UML diagrams, you can read this tutorial.