पाठ 15 / 25

UML Class Diagrams

Read and draw UML class diagrams with visibility, multiplicity and relationship arrows.

A shared picture of the design

UML class diagrams are the standard way to sketch object-oriented designs. A class is a box with three compartments: name, attributes and operations. Visibility markers: + public, - private, # protected, ~ package. Abstract classes and methods are shown in italics (or with {abstract}), interfaces with «interface», and static members underlined. Relationships use distinct arrows: inheritance (generalisation) is a solid line with a hollow triangle pointing to the parent; interface realisation is a dashed line with a hollow triangle; association is a plain solid line, optionally with an arrow for direction; aggregation has a hollow diamond at the whole's end; composition a filled diamond; dependency a dashed arrow. Multiplicity labels the ends: 1, 0..1, * or 1..*. You do not need every detail; use UML to communicate the important structure quickly in design discussions, documentation and exams. Text-based tools such as PlantUML and Mermaid generate diagrams from code-like descriptions, which fit nicely in version control.

A library system in PlantUML

Text that renders as a class diagram with the standard notation.

@startuml
interface Borrowable {
  +borrow(member: Member): Loan
}
abstract class Item {
  -id: String
  -title: String
  +{abstract} loanDays(): int
}
class Book extends Item {
  -isbn: String
  +loanDays(): int
}
class Dvd extends Item {
  +loanDays(): int
}
class Member {
  -name: String
}
class Loan {
  -due: LocalDate
}
class Library

Item ..|> Borrowable
Library "1" *-- "*" Item : owns >
Library "1" o-- "*" Member : registers >
Member "1" -- "0..*" Loan
Loan "*" --> "1" Item
@enduml

Diamonds sit on the whole

In aggregation and composition, the diamond is drawn at the end of the whole (the department, the order), not the part. Exams often check this detail.

त्वरित जाँच: In UML, how is composition shown?

  • A dashed line with a hollow triangle
  • A solid line with a hollow triangle
  • A solid line with a filled diamond at the whole's end
  • A dashed arrow
Answer

A solid line with a filled diamond at the whole's end — Composition uses a filled diamond; aggregation uses a hollow one.