SDE Path

Foundations

Class Relationships & UML for Machine Coding

Turn requirements into a class diagram fast — read and draw the four relationships before you write a line of code.

11 min5 steps · 7 checks
Step 1 of 50/7 checks correct

A machine-coding round rewards the candidate who spends the first sixty seconds sketching before typing. A single UML class box forces you to name the entity, decide what state it owns, and list the operations it exposes — three decisions you would otherwise make badly while already deep in syntax.

Every class in UML is drawn as one box split into three horizontal compartments:

class Account {
    private String id;
    private long balanceCents;

    public void deposit(long cents) { balanceCents += cents; }
    private void audit() { /* internal only */ }
}

The box for Account reads top to bottom like this:

  • Top compartment: the class name, Account, on its own.
  • Middle compartment: the fields, - id: String and - balanceCents: long.
  • Bottom compartment: the methods, + deposit(cents: long) and - audit().

The leading symbol is the visibility marker: + means public, - means private, and # means protected. So + deposit is callable by anyone, while - audit is internal. Reading a box, you learn an entity's whole contract at a glance — its name, its state, and the operations outsiders may invoke — without scrolling through an implementation. That is why the sketch beats diving straight into code: it makes you commit to responsibilities and encapsulation up front, where they are cheap to change.

Quick check

In a UML class box, a method written as # recalculate() has what visibility?

Quick check

Why do experienced candidates sketch the three-compartment box before writing code?