optional · CSS study notes
UML — Class Diagram Notation for OOP
UML — Unified Modeling Language (Class Diagram Notation)
Why this page exists: Association, Aggregation, Composition, and Inheritance (covered in code on "6. Classes Advanced") are concepts. UML is the standard visual notation CS textbooks and CSS papers use to draw those same concepts. This page is the notation key — read it side-by-side with Chapter 6's "Object Relationships" section.
1. What is UML?
- UML (Unified Modeling Language) — a standardized, general-purpose visual modeling language for specifying, visualizing, constructing, and documenting the artifacts of a software system.
- Created by Grady Booch, Ivar Jacobson, and James Rumbaugh (the "Three Amigos") at Rational Software in the mid-1990s; now maintained by the OMG (Object Management Group).
- Not a programming language and not a method/process — it is a notation (a diagramming vocabulary) that is process-independent.
- Purpose in OOP: lets a designer sketch classes, objects, and their relationships before writing code — a blueprint.
1.1 Types of UML Diagrams (classification)
- Structural diagrams — show the static structure of a system:
- Class diagram ← most important for CSS OOP paper (this page's focus)
- Object diagram
- Component diagram
- Deployment diagram
- Package diagram
- Behavioral diagrams — show the dynamic/runtime behavior:
- Use Case diagram
- Sequence diagram
- Activity diagram
- State (Statechart) diagram
- Collaboration/Communication diagram
MCQ Trap: UML has two main categories of diagrams — Structural and Behavioral — not one. Class diagrams are structural; Sequence and Activity diagrams are behavioral.
2. The Class Box — Three-Compartment Notation
A class in UML is drawn as a rectangle divided into three horizontal compartments:
| Compartment | Contains | Example |
|---|---|---|
| 1. Top | Class name (bold, centered; italics if abstract) | Student |
| 2. Middle | Attributes (data members): visibility name : type | • rollNo : int |
| 3. Bottom | Operations (member functions): visibility name(params) : returnType | • getMarks() : float |
┌─────────────────────────────┐
│ Student │ ← Class name
├─────────────────────────────┤
│ - rollNo : int │ ← Attributes
│ - name : string │
│ # marks : float │
├─────────────────────────────┤
│ + getMarks() : float │ ← Operations
│ + setName(n : string) : void │
└─────────────────────────────┘
- An abstract class name is written in italics (or tagged
{abstract}). - An interface is shown with the guillemet stereotype
«interface»above the name. - A static member is shown underlined.
3. Visibility (Access) Symbols
| Symbol | Meaning | Equivalent C++ keyword |
|---|---|---|
| + | Public | public |
| - | Private | private |
| # | Protected | protected |
| ~ | Package / default | (no direct C++ equivalent — Java default) |
MCQ Trap:
#is protected, not private.-is private.~(package) is a Java concept most C++-focused CSS papers rarely test but may appear as a distractor option.
4. Multiplicity Notation
Written at each end of a relationship line — tells how many objects of one class relate to how many of another.
| Notation | Meaning |
|---|---|
1 | Exactly one |
0..1 | Zero or one (optional) |
* or 0..* | Zero or many |
1..* | One or many (at least one) |
n | Exactly n |
n..m | Between n and m |
Example: Department 1 ────── 1..* Professor reads as "one Department has one-or-more Professors."
5. Relationship Notation — The Big Table
This is the direct visual counterpart to the Object Relationships table on Chapter 6. Same four concepts, now drawn as lines:
| Relationship | UML Line/Arrow | Arrowhead / Decoration | Meaning | Chapter 6 Equivalent |
|---|---|---|---|---|
| Association | Plain solid line | None (or plain open arrow if navigation direction matters) | "Uses-a" — general relationship, no ownership | Association (Teacher–Student) |
| Aggregation | Solid line | Hollow/open diamond ◇ at the "whole" end | "Has-a", weak — part can exist independently | Aggregation (Department–Professor) |
| Composition | Solid line | Filled/solid diamond ◆ at the "whole" end | "Has-a", strong — part's lifetime tied to whole | Composition (Car–Engine) |
| Inheritance / Generalization | Solid line | Hollow/open triangle △ pointing to the base (parent) class | "Is-a" — child inherits from parent | Inheritance (Ch. 8, 9) |
| Realization / Interface Implementation | Dashed line | Hollow triangle △ pointing to the interface | A class implements an interface's contract | Abstract Class / Pure Virtual Functions (Ch. 14, 15) |
| Dependency | Dashed line | Open arrow (stick) → | One class temporarily "uses" another (e.g., as a function parameter), weakest relationship | — |
5.1 Diamond memory hook
- ◇ Hollow diamond = Aggregation = "loose" ownership. Think of the diamond as "not fully colored in" — the relationship isn't fully committed, the part can survive without the whole.
- ◆ Filled diamond = Composition = "tight" ownership. The diamond is "solid/complete" — just like the part's lifetime is completely locked to the whole.
- The diamond always sits next to the "whole"/container class, never the "part" class.
5.2 Visual layout for the three core relationships
Association: Teacher ──────────────── Student
Aggregation: Department ◇──────────── Professor
(whole) (part, can exist independently)
Composition: Car ◆──────────────────── Engine
(whole) (part, destroyed with whole)
Inheritance: Animal △───────────────── Dog
(base) (hollow triangle (derived)
points to base)
Realization: «interface»
Shape △┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ Circle
(dashed + hollow (implements)
triangle)
6. Worked Example — Mapping C++ Code to a UML Diagram
Using the exact classes from Chapter 6's Object Relationships section:
class Student { };
class Teacher {
public:
void teach(Student& s); // Association
};
class Professor { };
class Department {
vector<Professor*> professors; // Aggregation — pointers, not owned
};
class Engine { };
class Car {
Engine engine; // Composition — owned member object
};
Equivalent UML:
Teacher ──────────────────── Student
(association: "teaches")
Department ◇──── 1..* ──── Professor
(aggregation)
Car ◆──── 1 ──── Engine
(composition)
7. Class Diagram vs Object Diagram
| Aspect | Class Diagram | Object Diagram |
|---|---|---|
| Shows | Classes (blueprints) and their static relationships | Actual object instances at a moment in time (a "snapshot") |
| Naming | ClassName | objectName : ClassName, underlined |
| When used | Design phase — structure of the system | Illustrating a specific runtime scenario |
8. Where UML Fits in the SDLC
- Drawn primarily during the Design phase of the Software Development Life Cycle (see the Software Engineering page for full SDLC coverage).
- Acts as a communication bridge between requirements (what the system must do) and implementation (the actual code).
- Class diagrams map almost directly onto C++/Java class declarations — this is why CSS OOP papers pair UML questions with code-writing questions.
Exam Angle
- CSS Computer Science (Paper-I) MCQs commonly test: the meaning of
+ - # ~, which diamond is hollow vs filled, and which arrow means inheritance vs interface realization. - A frequently asked short question: "Differentiate between Aggregation and Composition using UML notation." — Answer using the hollow-vs-filled diamond distinction plus the Chapter 6 lifetime-dependency argument.
- Be ready to draw a simple class box for a given scenario (e.g., a
BankAccountclass) with correct visibility symbols.
MCQ Traps
- ❌ "A filled diamond represents Association" → Wrong. Filled diamond = Composition. Association has no diamond at all.
- ❌ "The triangle for inheritance is filled/solid" → Wrong. The inheritance (generalization) triangle is hollow/open. A filled/solid arrowhead is not used for inheritance in standard UML.
- ❌ "
#means private" → Wrong.#= protected.-= private. - ❌ "Dependency and Association use the same line style" → Wrong. Association is a solid line; Dependency is a dashed line.
- ❌ "Realization and Inheritance both use solid lines" → Wrong. Inheritance = solid line + hollow triangle. Realization = dashed line + hollow triangle.
Important MCQs
-
Who are considered the primary creators ("Three Amigos") of UML?
Answer: Grady Booch, Ivar Jacobson, and James Rumbaugh
-
In a UML class box, which compartment lists the operations?
Answer: The bottom (third) compartment
-
Which visibility symbol represents a protected member?
A) + B) - C) # D) ~
-
A hollow diamond in a UML class diagram represents:
A) Composition B) Aggregation C) Inheritance D) Dependency
-
Which arrowhead style represents inheritance (generalization)?
Answer: A hollow/open triangle pointing toward the base class
-
What distinguishes Realization from Inheritance notation?
Answer: Realization uses a dashed line with a hollow triangle; Inheritance uses a solid line with a hollow triangle
-
Multiplicity
1..*means:Answer: One or many (at least one)
-
A static member in a UML class diagram is shown:
Answer: Underlined
-
Which UML diagram type is classified as "behavioral" rather than "structural"?
A) Class diagram B) Sequence diagram C) Component diagram D) Deployment diagram
-
In
Car ◆── Engine, which class is the "whole"?Answer: Car (the diamond sits next to the whole/container class)
Self-Test Questions
- Draw the three-compartment class box for a
BankAccountclass with a privatebalance(double) and a publicdeposit(amount : double) : voidmethod. - Explain, using UML notation only (no code), why Aggregation uses a hollow diamond and Composition uses a filled diamond.
- What is the visual difference between how UML represents Inheritance and how it represents Interface Realization?
- If
Library ◇── 0..* ── Book, what does the0..*tell you about how many books a library can have? - Convert the
Department/Professoraggregation code from Chapter 6 into a one-line UML relationship diagram, including multiplicity.
Cross-reference: for the code-level treatment of these same relationships (Association/Aggregation/Composition with working C++ examples and their own MCQs), see the "Object Relationships, Side by Side" section on "6. Classes Advanced."