Object Diagrams: A Snapshot of Real Objects
A class diagram says what can exist; an object diagram shows what does exist at one moment, such as this Saturday's three rentals with their real values, so you can see links the class diagram only allows and catch the ones it forbids.
Apprentice · Software Development · OOP · Updated
After this lesson you can
- Draw an object diagram of real objects and their values at one moment
- Tell a class diagram from an object diagram, and check one against the other
Key takeaways
- A class diagram is the recipe; an object diagram is one meal made from it: the real objects at one moment, with their values.
- An object's box is titled name : Class, underlined, and shows values (rider = "Maya"), not types.
- Lines between objects are links: this rental really points at that bike. There are no multiplicities; you count what's there.
- An object diagram can show a moment that breaks the class diagram's rules, such as one bike on two rentals.
Before this: How Many, and Who Owns It: Multiplicity and the Two Diamonds.
Last Saturday, Sam and Omar rode off on the same e-bike. Not really, since there's only one of it, but according to the desk's records they both had it, at the same time, for an afternoon. The code had compiled. The class diagram looked fine: it even says, right on the line, that a rentable is on 0..1 rentals at a time. And yet there it was.
A class diagram can't show this kind of mistake, because it isn't about any particular Saturday. It says what can exist: rentals, bikes, how they may connect. To see what does exist at one moment, which rental points at which bike and what's in each field, you need a different picture: an object diagram.
Saturday at the desk, in code
Here's a good Saturday: three rentals, added to the desk, and the day's total. The Desk keeps the day's rentals (the * from the last lesson's diagram):
Desk saturday = new Desk();
Rental maya = new Rental("Maya", new List<IRentable> { new Bike("4821") }, 2);
Rental sam = new Rental("Sam", new List<IRentable> { new EBike("7310") }, 3);
Rental dana = new Rental("Dana", new List<IRentable> { new Scooter() }, 1);
saturday.Add(maya);
saturday.Add(sam);
saturday.Add(dana);
foreach (Rental rental in saturday.Rentals)
{
Console.WriteLine( quot;{rental.Rider}: {string.Join(", ", rental.Items.Select(item => item.Name))}, {rental.Hours}h, ${rental.Total()}");
}
Console.WriteLine( quot;Saturday: ${saturday.Total()}");
interface IRentable { string Name { get; } int Price(int hours); }
class CodeLock { private string code; public CodeLock(string code) { this.code = code; } }
class Bike : IRentable
{
private CodeLock codeLock;
public Bike(string lockCode) { codeLock = new CodeLock(lockCode); }
public virtual string Name => "Bike";
public virtual int Price(int hours) => 10 * hours;
}
class EBike : Bike
{
public EBike(string lockCode) : base(lockCode) { }
public override string Name => "E-bike";
public override int Price(int hours) => 15 * hours;
}
class Scooter : IRentable { public string Name => "Scooter"; public int Price(int hours) => 8 * hours; }
class Rental
{
public string Rider { get; }
public List<IRentable> Items { get; }
public int Hours { get; }
public Rental(string rider, List<IRentable> items, int hours) { Rider = rider; Items = items; Hours = hours; }
public int Total() => Items.Sum(item => item.Price(Hours));
}
class Desk
{
public List<Rental> Rentals { get; } = new List<Rental>();
public void Add(Rental rental) => Rentals.Add(rental);
public int Total() => Rentals.Sum(rental => rental.Total());
}
Maya: Bike, 2h, $20
Sam: E-bike, 3h, $45
Dana: Scooter, 1h, $8
Saturday: $73
interface Rentable { String name(); int price(int hours); }
class CodeLock { private String code; CodeLock(String code) { this.code = code; } }
class Bike implements Rentable {
private CodeLock lock;
Bike(String lockCode) { lock = new CodeLock(lockCode); }
public String name() { return "Bike"; }
public int price(int hours) { return 10 * hours; }
}
class EBike extends Bike {
EBike(String lockCode) { super(lockCode); }
public String name() { return "E-bike"; }
public int price(int hours) { return 15 * hours; }
}
class Scooter implements Rentable {
public String name() { return "Scooter"; }
public int price(int hours) { return 8 * hours; }
}
class Rental {
String rider;
List<Rentable> items;
int hours;
Rental(String rider, List<Rentable> items, int hours) { this.rider = rider; this.items = items; this.hours = hours; }
public int total() { return items.stream().mapToInt(item -> item.price(hours)).sum(); }
}
class Desk {
List<Rental> rentals = new ArrayList<>();
public void add(Rental rental) { rentals.add(rental); }
public int total() { return rentals.stream().mapToInt(Rental::total).sum(); }
}
void main() {
Desk saturday = new Desk();
Rental maya = new Rental("Maya", List.of(new Bike("4821")), 2);
Rental sam = new Rental("Sam", List.of(new EBike("7310")), 3);
Rental dana = new Rental("Dana", List.of(new Scooter()), 1);
saturday.add(maya);
saturday.add(sam);
saturday.add(dana);
for (Rental rental : saturday.rentals) {
List<String> names = rental.items.stream().map(Rentable::name).toList();
IO.println(rental.rider + ": " + String.join(", ", names) + ", " + rental.hours + "h, quot; + rental.total());
}
IO.println("Saturday: quot; + saturday.total());
}
Maya: Bike, 2h, $20
Sam: E-bike, 3h, $45
Dana: Scooter, 1h, $8
Saturday: $73
class CodeLock:
def __init__(self, code): self._code = code
class Bike:
name = "Bike"
def __init__(self, lock_code): self._lock = CodeLock(lock_code)
def price(self, hours): return 10 * hours
class EBike(Bike):
name = "E-bike"
def price(self, hours): return 15 * hours
class Scooter:
name = "Scooter"
def price(self, hours): return 8 * hours
class Rental:
def __init__(self, rider, items, hours):
self.rider = rider
self.items = items
self.hours = hours
def total(self):
return sum(item.price(self.hours) for item in self.items)
class Desk:
def __init__(self): self.rentals = []
def add(self, rental): self.rentals.append(rental)
def total(self): return sum(rental.total() for rental in self.rentals)
saturday = Desk()
maya = Rental("Maya", [Bike("4821")], 2)
sam = Rental("Sam", [EBike("7310")], 3)
dana = Rental("Dana", [Scooter()], 1)
saturday.add(maya)
saturday.add(sam)
saturday.add(dana)
for rental in saturday.rentals:
names = ", ".join(item.name for item in rental.items)
print(f"{rental.rider}: {names}, {rental.hours}h, ${rental.total()}")
print(f"Saturday: ${saturday.total()}")
Maya: Bike, 2h, $20
Sam: E-bike, 3h, $45
Dana: Scooter, 1h, $8
Saturday: $73
class CodeLock {
#code;
constructor(code) { this.#code = code; }
}
class Bike {
name = "Bike";
#lock;
constructor(lockCode) { this.#lock = new CodeLock(lockCode); }
price(hours) { return 10 * hours; }
}
class EBike extends Bike {
name = "E-bike";
price(hours) { return 15 * hours; }
}
class Scooter {
name = "Scooter";
price(hours) { return 8 * hours; }
}
class Rental {
constructor(rider, items, hours) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
total() { return this.items.reduce((sum, item) => sum + item.price(this.hours), 0); }
}
class Desk {
rentals = [];
add(rental) { this.rentals.push(rental); }
total() { return this.rentals.reduce((sum, rental) => sum + rental.total(), 0); }
}
const saturday = new Desk();
const maya = new Rental("Maya", [new Bike("4821")], 2);
const sam = new Rental("Sam", [new EBike("7310")], 3);
const dana = new Rental("Dana", [new Scooter()], 1);
saturday.add(maya);
saturday.add(sam);
saturday.add(dana);
for (const rental of saturday.rentals) {
const names = rental.items.map((item) => item.name).join(", ");
console.log(`${rental.rider}: ${names}, ${rental.hours}h, ${rental.total()}`);
}
console.log(`Saturday: ${saturday.total()}`);
Maya: Bike, 2h, $20
Sam: E-bike, 3h, $45
Dana: Scooter, 1h, $8
Saturday: $73
interface Rentable {
name: string;
price(hours: number): number;
}
class CodeLock {
private code: string;
constructor(code: string) { this.code = code; }
}
class Bike implements Rentable {
name = "Bike";
private lock: CodeLock;
constructor(lockCode: string) { this.lock = new CodeLock(lockCode); }
price(hours: number): number { return 10 * hours; }
}
class EBike extends Bike {
name = "E-bike";
price(hours: number): number { return 15 * hours; }
}
class Scooter implements Rentable {
name = "Scooter";
price(hours: number): number { return 8 * hours; }
}
class Rental {
rider: string;
items: Rentable[];
hours: number;
constructor(rider: string, items: Rentable[], hours: number) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
total(): number { return this.items.reduce((sum, item) => sum + item.price(this.hours), 0); }
}
class Desk {
rentals: Rental[] = [];
add(rental: Rental): void { this.rentals.push(rental); }
total(): number { return this.rentals.reduce((sum, rental) => sum + rental.total(), 0); }
}
const saturday = new Desk();
const maya = new Rental("Maya", [new Bike("4821")], 2);
const sam = new Rental("Sam", [new EBike("7310")], 3);
const dana = new Rental("Dana", [new Scooter()], 1);
saturday.add(maya);
saturday.add(sam);
saturday.add(dana);
for (const rental of saturday.rentals) {
const names = rental.items.map((item) => item.name).join(", ");
console.log(`${rental.rider}: ${names}, ${rental.hours}h, ${rental.total()}`);
}
console.log(`Saturday: ${saturday.total()}`);
Maya: Bike, 2h, $20
Sam: E-bike, 3h, $45
Dana: Scooter, 1h, $8
Saturday: $73
The recipe and the meal
A class diagram is a recipe: it says what goes into a rental and how the parts fit, and it's true every day. An object diagram is one meal made from it: these rentals, these bikes, these values, at one moment. The program above makes one meal: after its last add, here is everything in memory, drawn.

How an object diagram is drawn
An object diagram uses boxes and lines too, but each one means something more specific:
- Each box is one object, titled name : Class, underlined:
maya : Rentalis read "maya, a Rental". The underline is what says "an object, not a class". An object the code never names is drawn with the colon only:: Bikeis "a Bike". - Each box shows values, not types:
rider = "Maya"andhours = 2, notrider: string. Leave out the fields that don't matter to the question you're asking, as you would in a class diagram. - Each line is a link: this object really holds that one, right now. There are no arrows' meanings to learn, no diamonds and no multiplicities. Instead of
*, you see what's there: three lines from the desk, because there are three rentals.
Try it. At 9 a.m. the desk has one rental: Leo, two bikes and an e-bike for 2 hours. Sketch the object diagram. (A box : Desk with one line to a box leo : Rental, with rider = "Leo" and hours = 2; three lines from it, to : Bike, another : Bike and : EBike; and a : CodeLock linked to each of the three.)
What the snapshot shows that the recipe didn't
Look at the right-hand diagram again. Three things are visible there that the class diagram only implied:
- Sam's e-bike has a lock. The class diagram draws the lock on
Bikeonly, but an e-bike is a bike, so it inherited the lock. In the snapshot, there it is. - Each rental has its own bike. Three rentals, three separate objects.
- The
*turned into a number. Three links from the desk: this Saturday's count.
Checking one against the other
Now last Saturday's mistake. Drawn as an object diagram, the moment looked like this: two rental boxes, sam : Rental and omar : Rental, both with a line to the same : EBike box. The clerk's screen had made one EBike object and handed it to both rentals.

Hold it next to the class diagram and the problem jumps out: the class diagram says a rentable is on 0..1 rentals, and this e-bike is on two. That's the habit to build. The class diagram states the rules; an object diagram of a real moment can be checked against them, line by line, the way you'd check a receipt against the price list.
Where you'll use it again
Some patterns only make sense as objects. In Pass 2 you'll meet a shipment made of boxes that hold other boxes, and in Pass 3 a slow weather service wrapped in layers. Their class diagrams look small and puzzling; their object diagrams show the tree and the layers plainly. When a diagram puzzles you, ask what the objects look like at one moment, and draw that.
Check yourself
2 questions to try on the lesson page.
So what
When you need to know what the program holds right now, not what it could hold, draw the objects. It's the quickest way to see a link that shouldn't be there.