Objects and Classes
Objects bundle state, behavior, and identity; classes are the blueprints that define them. The foundation every other design concept builds on.
Every design concept on this platform — patterns, principles, diagrams — ultimately describes how objects and classes should relate. Get this foundation precise and the rest follows.
Objects: state, behavior, identity
An object is a runtime thing. It exists in memory while the program runs, and it has three defining characteristics:
- State — the current values of its fields. An account object holds a balance of 240.00 right now; after a withdrawal, its state changes.
- Behavior — the methods you can call on it.
deposit,withdraw,getBalance. Behavior is how state changes in controlled ways. - Identity — each object is distinct from every other object, even when their states are equal. Two accounts both holding 100.00 are still two accounts. Deposit into one and the other is unaffected.
Identity is the point beginners miss most often. Equality compares state; identity asks whether two references point at the same object. Most languages make the distinction explicit: === on object references in TypeScript, == versus equals() in Java.
Classes: the blueprint
A class is a compile-time construct: a named definition that declares which attributes every instance will have and defines the methods that operate on them. The class itself holds no balance and processes no withdrawal — it describes what its instances can do.
Creating an object from a class is instantiation. The new keyword allocates a fresh object, runs the constructor to establish initial state, and returns a reference with its own identity.
class BankAccount {
private balance = 0;
constructor(readonly owner: string) {}
deposit(amount: number): void {
if (amount <= 0) throw new Error("Deposit must be positive");
this.balance += amount;
}
withdraw(amount: number): void {
if (amount > this.balance) throw new Error("Insufficient funds");
this.balance -= amount;
}
getBalance(): number {
return this.balance;
}
}
const aliceAccount = new BankAccount("Alice");
const bobAccount = new BankAccount("Bob");
aliceAccount.deposit(100);
bobAccount.deposit(100);
aliceAccount.getBalance(); // 100
bobAccount.getBalance(); // 100
console.log(aliceAccount === bobAccount); // false — equal state, distinct identityOne class, two objects. Withdrawing from aliceAccount never touches bobAccount, because state belongs to the instance, not the class.
| Class | Object | |
|---|---|---|
| Exists | At design time, in source code | At runtime, in memory |
| Quantity | One definition | Any number of instances |
| Contains | Attribute declarations, method definitions | Actual field values plus an identity |
| Analogy | Architectural blueprint | A building constructed from it |
In UML, classes are drawn as three-compartment boxes (name, attributes, operations); the notation and its formal semantics are covered in class diagrams. Objects appear far less often in diagrams — most modeling happens at the class level precisely because one class describes unboundedly many instances.
Why bundle data with behavior
You could store the balance in a plain record and put withdraw logic in a free function somewhere else. Bundling them in one class buys you two concrete things:
Locality of change. When the overdraft rules change, there is exactly one place to edit: BankAccount. If the logic were scattered across every module that touches a balance, each rule change becomes a codebase-wide hunt.
Invariant protection. balance is private, so the only paths that mutate it are deposit and withdraw — both of which enforce the rule that a balance never goes negative. No caller, however careless, can break that invariant. The class is a fence around its own consistency.
This is the seed of encapsulation, and it scales up into a general design instinct: put behavior where the data lives. That instinct has a name — Information Expert — and it is the first question to ask when deciding which class should own a new method. It also previews Single Responsibility: a class that owns one cluster of data and its rules tends to have exactly one reason to change.
When to introduce a class
Reach for it
- A group of fields must change together under shared rules
- You keep passing the same cluster of parameters between functions
- An invariant must hold no matter which code path mutates the data
Avoid it
- A single pure function with no state would do the job
- The class would only hold data while other code does all the real work
- You are mirroring a database table one-to-one instead of modeling behavior
Common beginner mistakes
The anemic model. A class that is nothing but private fields plus a getter and setter for each one, while the actual logic lives in a separate AccountService. This keeps the syntax of objects but discards both benefits above: rules scatter, and every setter is a hole in the invariant fence. Getters are fine; a class whose only behavior is getters and setters is a warning sign, not a rule violation — sometimes a plain data carrier (a DTO) is genuinely all you need at a boundary.
Classes named after verbs. PaymentProcessor, OrderManager, DataHandler. A verb-shaped name usually means the class is a pile of procedures in search of a home, and the real domain objects (Payment, Order) end up anemic. Name classes after the things in your domain and give those things their own behavior. Heuristic, not law — some verb-named classes are legitimate, such as a factory or a strategy.
Class equals database table. Tables model storage: flat rows, foreign keys, normalization pressure. Classes model behavior: methods, invariants, references to other objects. The two often correlate but are rarely one-to-one — one class may span tables, one table may back several classes, and some classes never touch the database at all. Design your objects from behavior first; map to storage second. How tables are actually designed is the subject of ER modeling.
Practice
Model a library lending desk. Write a Member class with a private list of current loans and a rule: a member may hold at most five loans, and never two copies of the same book. Implement borrow(bookId) and returnBook(bookId) so the invariants are enforceable only inside the class — no public setter for the loan list. Instantiate two members, have both borrow the same book id, and verify their loan states are independent even when equal. Then write the anemic version (public loan list, logic in a LoanService) and note exactly which lines of calling code could now corrupt the invariant.