Skip to content

PlantUML

Class Diagrams

Relationships between Classes

Relations between classes are defined using the following symbols :

Type Symbol Purpose
Extension < | -- Specialization of a class in a hierarchy (inheritance). The child class inherits all properties and behaviors from the parent class.
Implementation < | .. Realization of an interface by a class. The class commits to implementing all methods defined in the interface.
Composition *-- The part cannot exist without the whole. Strong ownership where the lifecycle of the part is managed by the whole (e.g., Engine is part of Car).
Aggregation o-- The part can exist independently of the whole. Weak ownership where parts can outlive the whole (e.g., Student can exist without University).
Dependency --> The object uses another object temporarily, typically as a method parameter or local variable. Changes to the used class may affect the dependent class.
Dependency ..> A weaker form of dependency, often used to show that a class creates or instantiates another class, or uses it in a less direct way.

It is possible to replace -- by .. to have a dotted line.

@startuml
Vehicle <|-- Car
Vehicle <|-- Motorcycle

interface Drivable
Drivable <|.. Car
Drivable <|.. Motorcycle

Car *-- Engine
Car *-- Transmission

Car o-- Driver

Car ..> FuelStation
Car --> GPS : uses for navigation

class Vehicle {
  - brand: String
  - model: String
  + start(): void
  + stop(): void
}

class Car {
  - numberOfDoors: int
  + drive(): void
  + refuel(station: FuelStation): void
}

class Motorcycle {
  - hasWindshield: boolean
  + ride(): void
}

interface Drivable {
  + accelerate(): void
  + brake(): void
}

class Engine {
  - horsepower: int
  - fuelType: String
  + ignite(): void
}

class Transmission {
  - gears: int
  + shiftGear(gear: int): void
}

class Driver {
  - name: String
  - licenseNumber: String
  + getName(): String
}

class GPS {
  + getRoute(destination: String): Route
}

class FuelStation {
  + refuel(): void
}
@enduml

Define Visibility of Class Members

When you define methods or fields, you can use characters to define the visibility of the corresponding item:

Character Visibility
- private
# protected
~ package private
+ public
@startuml
class Dummy {
 -privateField
 #protectedField
 ~packagePrivateMethod()
 +publicMethod()
}
@enduml