PlantUML is a powerful tool to create class diagrams that help in visualizing and designing object-oriented software. Below is a comprehensive guide to creating class diagrams with PlantUML for clear object-oriented design.
1. What is PlantUML?
PlantUML is a text-based tool for creating UML (Unified Modeling Language) diagrams. It uses simple plain-text descriptions to generate visual representations of UML diagrams (including class diagrams, sequence diagrams, etc.).
2. Setting Up PlantUML
Option 1: Standalone
- Install Java. PlantUML requires Java (minimum version 1.8).
- Download the PlantUML JAR file from PlantUML’s official website.
- Use a text editor to write PlantUML code.
- Generate diagrams by running:
java -jar plantuml.jar your-diagram-file.puml
Option 2: IntelliJ IDEA Integration
- Install the PlantUML Integration plugin in IntelliJ IDEA:
- Go to
File > Settings > Plugins. - Search for “PlantUML Integration”.
- Install the plugin and restart IntelliJ IDEA.
- Go to
- Write
.pumlfiles directly in IntelliJ and render diagrams using the integrated PlantUML viewer.
3. Components of a PlantUML Class Diagram
Basic Syntax
PlantUML class diagrams are defined inside @startuml and @enduml tags. Here’s an example:
@startuml
class ClassName {
+ PublicAttribute
- PrivateAttribute
# ProtectedAttribute
+ method(): ReturnType
}
@enduml
Key Elements
| Symbol | Meaning |
|---|---|
+ |
Public |
- |
Private |
# |
Protected |
< and > |
Generics/Parameterized Class |
.. or -- |
Relationships/Dependencies |
4. Example of a Class Diagram
Below is an example of modeling an object-oriented system with PlantUML:
@startuml
class Person {
- id: int
+ name: String
+ email: String
+ getContactInfo(): String
}
class Employee {
- employeeId: int
+ department: String
+ getRole(): String
}
class Manager {
+ manages: List<Employee>
+ assignTask(task: Task): void
}
Person <|-- Employee
Employee <|-- Manager
@enduml
Key Explanation:
- Class Definitions:
- Each class (e.g.,
Person,Employee,Manager) is defined with attributes and methods.
- Each class (e.g.,
- Inheritance:
- The arrow
<|--shows inheritance relationships (e.g.,Employeeis derived fromPerson).
- The arrow
- Associations:
- Use arrows (e.g., associations to depict relationships between objects).
5. Advanced Features
Adding Interfaces
@startuml
interface PaymentProcessor {
+ processPayment(amount: Double): Boolean
}
class CreditCardPayment {
+ creditCardNumber: String
+ expiryDate: String
}
PaymentProcessor <|.. CreditCardPayment
@enduml
- You can define an interface using the
interfacekeyword. - Use
<|..to implement interfaces.
Abstract Classes
@startuml
abstract class Shape {
+ draw(): void
}
class Circle {
+ radius: Double
}
class Rectangle {
+ width: Double
+ height: Double
}
Shape <|-- Circle
Shape <|-- Rectangle
@enduml
- Define abstract classes using the
abstractkeyword. - Use
<|--to show that classes inherit from the abstract class.
Relationships
| Type | Syntax | Example |
|---|---|---|
| Inheritance | < | --or`< |
|
| Composition | *--> or *-- |
House *--> Room |
| Aggregation | o--> or o-- |
Team o--> Player |
| Association | -- or <..> |
Library -- Borrower |
Generic Classes
@startuml
class Stack<T> {
+ push(item: T): void
+ pop(): T
}
@enduml
- Generics are supported using angle brackets (
<T>).
6. Best Practices for Clear Diagrams
- Keep It Simple: Focus on key parts of the design. Avoid overcrowding diagrams with unnecessary details.
- Use Annotations: Add comments and notes to improve readability. You can annotate using:
note "This is a general note" as N1 - Organize Classes: Use packages or group related classes together:
package "Model" { class User class Role } - Use Relationships Effectively: Clearly specify inheritance, associations, aggregations, and compositions for better understanding.
7. Generate the Diagram
After writing the .puml file, generate the diagram:
- Use PlantUML JAR, IntelliJ IDEA, or other tools like PlantUML online editor.
By following these steps, you can create clear, well-structured class diagrams to visualize object-oriented designs effectively.

