> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/system-design/system-design-methodology/diagrams/uml-diagrams/plantuml/class-diagram.md).

# Class Diagram

## About

A Class Diagram is a structural UML (Unified Modeling Language) diagram that visually represents the blueprint of a system's classes, their attributes, methods, and relationships. It is widely used in object-oriented programming to design and document system architecture.

{% hint style="success" %}
Refer to the official documentation for more details - <https://plantuml.com/class-diagram>
{% endhint %}

### **Elements of a Class Diagram**

1. **Classes**
   * Represented as a **rectangle** with three sections:
     * **Class Name** (e.g., `Customer`)
     * **Attributes** (e.g., `+name: String`)
     * **Methods** (e.g., `+getCustomerDetails(): String`)
   * Example:

     ```
     +-----------------+
     |   Customer      |
     +-----------------+
     | - id: int       |
     | - name: String  |
     | - email: String |
     +-----------------+
     | +getDetails()   |
     | +updateEmail()  |
     +-----------------+
     ```
2. **Relationships**
   * Defines how classes interact with each other.
   * **Association** (`--`): Direct connection between classes.
   * **Aggregation** (`<>--`): A whole-part relationship where the part can exist independently.
   * **Composition** (`*--`): A stronger form of aggregation; the part cannot exist independently.
   * **Inheritance** (`<|--`): Denotes that one class is a subtype of another.
   * **Dependency** (`..>`): A weak relationship where one class depends on another.
3. **Modifiers**
   * **Public (`+`)**: Accessible from anywhere.
   * **Private (`-`)**: Accessible only within the class.
   * **Protected (`#`)**: Accessible within the class and its subclasses.
   * **Package (`~`)**: Accessible within the same package.

## 1. Banking System

This diagram represents a **Banking System**, including multiple relationships such as **inheritance, composition, aggregation, and dependencies**.

{% hint style="success" %}

* **Inheritance (`<|--`)**: `SavingsAccount` and `CurrentAccount` inherit from `Account`.
* **Association (`--`)**: `Customer` owns multiple `Accounts`.
* **Composition (`o--`)**: `Bank` is composed of multiple `Accounts`.
* **Multiplicity (`0..*`, `1`)**: Defines relationships between classes.
  {% endhint %}

```plant-uml
@startuml
title Banking System Class Diagram

class Account {
  - accountNumber: String
  - balance: double
  + deposit(amount: double)
  + withdraw(amount: double)
}

class SavingsAccount {
  - interestRate: double
  + addInterest()
}

class CurrentAccount {
  - overdraftLimit: double
  + checkOverdraft()
}

class Transaction {
  - transactionId: String
  - amount: double
  - date: Date
  + execute()
}

class Customer {
  - name: String
  - email: String
  - phoneNumber: String
  + getDetails()
}

class Bank {
  - name: String
  - branch: String
  + openAccount(customer: Customer)
  + closeAccount(account: Account)
}

Account <|-- SavingsAccount
Account <|-- CurrentAccount
Customer "1" -- "0..*" Account : owns
Account "1" o-- "*" Transaction : has
Bank "1" o-- "*" Account : manages

@enduml

```

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Ft1jBQAPQGQxkT7RlNINu%2Fplantuml-class-diagram-1.png?alt=media&amp;token=bde85921-ecb3-433a-981f-ab5dd725be97" alt="" width="507"><figcaption></figcaption></figure>

## 2. Hospital Management System

This represents **relationships between classes** in a **Hospital Management System**.

```plant-uml
@startuml
title Hospital Management System - Object Relationship Diagram

class Doctor {
    +name: String
    +specialization: String
}

class Patient {
    +name: String
    +age: int
}

class Appointment {
    +date: Date
    +time: Time
}

Doctor "1" -- "0..*" Appointment : schedules
Patient "1" -- "0..*" Appointment : books

@enduml
```

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2FuGKQbOHT5MNLBLQSOg45%2Fplantuml-class-diagram-2.png?alt=media&amp;token=27e7d4e9-a0f3-4d2f-89b5-3d80b2383938" alt="" width="389"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/system-design/system-design-methodology/diagrams/uml-diagrams/plantuml/class-diagram.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
