OBJECT-ORIENTED FOUNDATIONS / OBJECT DESIGN BRIEF

Objects and classes

A class is a blueprint that defines what data an object holds (fields) and what it can do (methods).

BeginnerPhase 01 / Topic 1 of 7ResponsibilitiesCollaborationsExtensibility
01

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).

Cookie cutter and cookies

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.

02

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.
03

Where it shows up in interviews

Entity modeling

Recognize it when: nouns in the requirements with data and rules.

  • Design a parking lot
  • Design a library system
Rich vs anemic models

Recognize it when: logic lives in services that poke at data objects.

  • Refactor an order system
  • Design a bank account
04

Where it is used in real software

Domain models

Frameworks like Spring and NestJS map classes such as Order and Customer to database tables and API payloads.

UI components

Each React or Android view instance is an object with its own state created from a shared definition.

Standard libraries

Java's ArrayList, HashMap, and LocalDate are classes; every list you create is an object.

05

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.
06

How it works, step by step

  1. 1
    Find the noun

    Account, Order, Vehicle: candidates for classes.

  2. 2
    List its state

    Fields it must remember: id, balance, owner.

  3. 3
    List its behavior

    What it does, not what is done to it: deposit, withdraw.

  4. 4
    Protect invariants in the constructor and methods

    Balance never negative; id never null.

  5. 5
    Create instances

    Each object has independent state.

07

Two accounts from one class

new BankAccount('A-1', 100) and new BankAccount('A-2', 50)

Step 1 / 4
OperationA-1 balanceA-2 balance
Create both10050
A-1.deposit(40)14050
A-2.withdraw(20)14030
A-2.withdraw(100)14030 (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.

08

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: 50
09

Complexity and performance

Object creationO(fields)

Allocation plus constructor.

Method callO(1) dispatch

Plus the method's own work.

10

Trade-offs

Rich objects vs data bags

Behavior on the object keeps rules in one place; plain data objects plus services are simpler for CRUD but scatter rules.

Mutability

Mutable objects are convenient but harder to share across threads; immutable objects are safer but create copies.

11

Variants and related techniques

Records / data classes

Java records, Kotlin data classes, and TypeScript types for pure data.

Prototype-based objects

JavaScript objects can inherit directly from other objects.

12

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.

13

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.

14

Practice problems

ProblemDifficultyWhat it trains
Model a Book and Member for a libraryEasyState and behavior.
Model a ShoppingCart with add/remove/totalEasyInvariants.