Overview
A class is a blueprint that defines what data an object holds (fields) and what it can do (methods). An object is one concrete instance of that class with its own state. A BankAccount class describes every account; your account with balance 500 is an object.
Good object-oriented design starts by putting data and the behavior that uses it in the same place. Instead of a function elsewhere that reads account.balance and subtracts, the account itself exposes withdraw(amount) and protects its own rules. Objects have identity (which one), state (current values), and behavior (methods).
The cutter (class) defines the shape. Each cookie (object) is made from it but has its own frosting and toppings (state). Eating one cookie does not affect the others.
When to use it
- Modeling real concepts that have both data and rules: accounts, orders, vehicles, tickets.
- Grouping related state so it cannot be changed inconsistently.
- Any LLD interview: the first step is identifying classes and their responsibilities.
Where it shows up in interviews
Recognize it when: nouns in the requirements with data and rules.
- Design a parking lot
- Design a library system
Recognize it when: logic lives in services that poke at data objects.
- Refactor an order system
- Design a bank account
Where it is used in real software
Frameworks like Spring and NestJS map classes such as Order and Customer to database tables and API payloads.
Each React or Android view instance is an object with its own state created from a shared definition.
Java's ArrayList, HashMap, and LocalDate are classes; every list you create is an object.
Key terms
- Class
- Blueprint describing fields and methods.
- Object / instance
- A concrete value created from a class.
- Constructor
- Initializes a new object into a valid state.
- Instance vs static members
- Belongs to each object vs shared by the class.
- Identity
- Two objects can have equal data yet be different objects.
How it works, step by step
- 1Find the noun
Account, Order, Vehicle: candidates for classes.
- 2List its state
Fields it must remember: id, balance, owner.
- 3List its behavior
What it does, not what is done to it: deposit, withdraw.
- 4Protect invariants in the constructor and methods
Balance never negative; id never null.
- 5Create instances
Each object has independent state.
Two accounts from one class
new BankAccount('A-1', 100) and new BankAccount('A-2', 50)
| Operation | A-1 balance | A-2 balance |
|---|---|---|
| Create both | 100 | 50 |
| A-1.deposit(40) | 140 | 50 |
| A-2.withdraw(20) | 140 | 30 |
| A-2.withdraw(100) | 140 | 30 (rejected: insufficient funds) |
NOWOperation: Create both | A-1 balance: 100 | A-2 balance: 50
Each object owns its state, and the class enforces the rule for all of them.
Implementation
class BankAccount { private balance: number; static readonly MIN_OPENING = 0; constructor(readonly id: string, openingBalance: number) { if (openingBalance < BankAccount.MIN_OPENING) throw new Error("Opening balance cannot be negative"); this.balance = openingBalance; } deposit(amount: number) { if (amount <= 0) throw new Error("Deposit must be positive"); this.balance += amount; } withdraw(amount: number) { if (amount > this.balance) throw new Error("Insufficient funds"); this.balance -= amount; } getBalance() { return this.balance; }} const a = new BankAccount("A-1", 100);const b = new BankAccount("A-2", 50);a.deposit(40); // a: 140, b: 50Complexity and performance
Allocation plus constructor.
Plus the method's own work.
Trade-offs
Behavior on the object keeps rules in one place; plain data objects plus services are simpler for CRUD but scatter rules.
Mutable objects are convenient but harder to share across threads; immutable objects are safer but create copies.
Variants and related techniques
Java records, Kotlin data classes, and TypeScript types for pure data.
JavaScript objects can inherit directly from other objects.
Common mistakes
- Public fields everyone mutates.
Fix: Expose behavior methods; keep fields private.
- Classes with only getters and setters.
Fix: Move the logic that uses the data into the class (tell, don't ask).
- Static everything.
Fix: Static methods cannot be substituted or mocked; use instances.
Interview questions
What is the difference between a class and an object?
A class is the definition of fields and methods; an object is a runtime instance of that class with its own state and identity.
What does 'tell, don't ask' mean?
Instead of asking an object for its data and making decisions outside it, tell the object what to do (account.withdraw(50)) so its rules stay inside.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Model a Book and Member for a library | Easy | State and behavior. |
| Model a ShoppingCart with add/remove/total | Easy | Invariants. |