Coursify

Mastering Low Level Design (LLD)

Mastering Class Diagrams

20 mins

Learn how to read and create Class Diagrams, the most critical UML tool for defining the static structure of an object-oriented system.

Learning Goals

  • Identify class attributes, methods, and visibility levels.
  • Differentiate between Association, Aggregation, and Composition.
  • Design complex system structures using Mermaid classDiagram syntax.

The Language of Structure: Class Diagrams

If HLD describes the islands in your system, a Class Diagram describes the architecture of the buildings on those islands. It is a static diagram that describes the structure of a system by showing its classes, their attributes, operations, and the relationships among objects.

Anatomy of a Class

A class in UML is typically represented by a rectangle divided into three compartments:

  1. Top: The Class Name.
  2. Middle: Attributes (fields) with their types and visibility.
  3. Bottom: Methods (operations) with their parameters and return types.

Visibility Notations:

  • + Public
  • - Private
  • # Protected
  • ~ Package/Internal

Mastering Relationships

The true power of a class diagram lies in showing how objects relate to one another. There are five primary relationship types you must master.

1. Generalization (Inheritance)

Indicates an "is-a" relationship. Represented by a solid line with a hollow arrowhead pointing to the parent.

2. Realization (Implementation)

Indicates a class implementing an interface. Represented by a dashed line with a hollow arrowhead.

3. Association

A basic "knows-a" relationship. Class A has a reference to Class B. Represented by a simple solid line.

4. Aggregation

A "part-of" relationship where the child can exist independently of the parent (e.g., a Department and its Professors). Represented by a hollow diamond.

5. Composition

A strong "part-of" relationship where the child cannot exist without the parent (e.g., a House and its Rooms). Represented by a filled diamond.

Visualizing Relationships with Mermaid

In the diagram above:

  • A University is composed of Departments (if the uni closes, departments are gone).
  • A Department aggregates Professors (if the department closes, professors still exist).
  • A Professor has an association with MathCourses.
  • MathCourse realizes the Course interface.

Knowledge Check

Question 1 of 3
Q1Single choice

Which relationship type indicates that the child object cannot exist independently of the parent?