OOP Interview Prep
OOP Fundamentals

Class & Object: The Blueprint and the Thing You Actually Build

Class & Object: The Blueprint and the Thing You Actually Build

LinkedIn Hook

Most developers can write a class. Far fewer can explain what a class actually is when an interviewer asks.

Here's the surprising part: a class is never in memory at runtime. Only the objects created from it are. That one distinction trips up a lot of candidates who have been writing class for years without thinking about what happens under the hood.

In this lesson you'll learn: what the difference is between a class and an object, how a constructor shapes every new instance, and the one interview trick question about classes that almost everyone gets wrong the first time.

Read the full lesson → [link] #OOP #JavaScript #SoftwareEngineering #InterviewPrep


Class & Object: The Blueprint and the Thing You Actually Build thumbnail


What You'll Learn

  • A class is a blueprint. An object is a live instance created from that blueprint — they are fundamentally different things.
  • The constructor runs once per object, at the moment new is called, and its job is to set initial state.
  • One class can produce unlimited independent objects, each with its own separate property values.

What Is a Class, Really?

Think of a class as a cookie cutter, not a cookie. The cutter defines the shape: it says every cookie from this mold will have these edges, these holes, this pattern. But the cutter itself is never eaten. You use it to stamp out as many cookies as you need, and each one exists independently.

In code, the class defines the structure and behavior. The objects are the actual things that live in memory, hold data, and respond to method calls.

// The class is the blueprint — it is not an object itself
class Car {
  constructor(make, model, year) {
    this.make = make;     // property set per instance
    this.model = model;
    this.year = year;
  }

  describe() {
    return `${this.year} ${this.make} ${this.model}`;
  }
}

// The objects are the real things created from the blueprint
const car1 = new Car("Toyota", "Camry", 2022);
const car2 = new Car("Honda", "Civic", 2023);

console.log(car1.describe()); // "2022 Toyota Camry"
console.log(car2.describe()); // "2023 Honda Civic"

console.log(car1 === car2);   // false — two separate objects in memory

car1 and car2 share the same blueprint (Car), but they are completely independent. Changing car1.year has zero effect on car2.year.


The Constructor: Setting Initial State

The constructor is a special method that JavaScript calls automatically the moment you write new ClassName(...). Its only job is to set up the initial state of that specific object. Once it finishes, you have a fully initialized instance ready to use.

class BankAccount {
  constructor(owner, initialBalance) {
    this.owner = owner;
    this.balance = initialBalance;
    this.transactions = [];        // each account gets its own empty array
    this.createdAt = new Date();   // timestamp set at creation time
  }

  deposit(amount) {
    this.balance += amount;
    this.transactions.push({ type: "deposit", amount });
  }

  getBalance() {
    return `${this.owner}: $${this.balance}`;
  }
}

const alice = new BankAccount("Alice", 1000);
const bob = new BankAccount("Bob", 500);

alice.deposit(200);

console.log(alice.getBalance());          // "Alice: $1200"
console.log(bob.getBalance());            // "Bob: $500"
console.log(alice.transactions.length);   // 1
console.log(bob.transactions.length);     // 0 — completely separate array

Notice the transactions array. Each object gets its own independent array because the constructor runs this.transactions = [] fresh for every new instance. This is critical to understand — and it leads directly into one of the most common interview traps.

Class & Object: The Blueprint and the Thing You Actually Build visual 1


Properties and Methods: Data vs Behavior

Every class has two kinds of members. Properties store data. Methods define behavior. The distinction sounds obvious, but interviews sometimes test it with trickier wording.

  • Property: a variable that belongs to an object (this.name, this.speed)
  • Method: a function that belongs to an object (this.drive(), this.describe())
class Rectangle {
  constructor(width, height) {
    // Properties: data stored on the instance
    this.width = width;
    this.height = height;
  }

  // Methods: behavior the instance can perform
  area() {
    return this.width * this.height;
  }

  perimeter() {
    return 2 * (this.width + this.height);
  }

  isSquare() {
    return this.width === this.height;
  }
}

const rect = new Rectangle(5, 10);
console.log(rect.area());       // 50
console.log(rect.perimeter());  // 30
console.log(rect.isSquare());   // false

const square = new Rectangle(7, 7);
console.log(square.isSquare()); // true

Methods are defined once on the class, but every instance can call them. JavaScript stores methods on the prototype (not duplicated per instance), so creating 1,000 Rectangle objects does not create 1,000 copies of the area function.

[UNIQUE INSIGHT]: Most tutorials skip explaining why methods live on the prototype. The answer is memory efficiency. If methods were stored directly on each instance (as own properties), every new Rectangle() call would copy the function into the new object. Prototype storage means all instances share one function reference, which matters when you instantiate classes at scale.


Creating Multiple Objects from One Class

One class can produce as many objects as you need. Each object is independent: it holds its own property values, and changes to one object never affect another.

class User {
  constructor(username, role) {
    this.username = username;
    this.role = role;
    this.isActive = true;
  }

  deactivate() {
    this.isActive = false;
  }

  toString() {
    return `[${this.role}] ${this.username} — active: ${this.isActive}`;
  }
}

const admin = new User("rakib", "admin");
const editor = new User("sarah", "editor");
const viewer = new User("tom", "viewer");

admin.deactivate();

console.log(admin.toString());  // "[admin] rakib — active: false"
console.log(editor.toString()); // "[editor] sarah — active: true"
console.log(viewer.toString()); // "[viewer] tom — active: true"

// Checking object identity
console.log(admin instanceof User);  // true
console.log(typeof admin);           // "object" — not "class"

admin, editor, and viewer are three separate objects in memory. Deactivating admin has no effect on the others. The instanceof operator confirms each one was built from the User class.

[PERSONAL EXPERIENCE]: In code reviews, a common mistake is treating objects from the same class as if they share state. Developers sometimes write a method that mutates this and then wonder why another variable seems unaffected. The answer is almost always that they were working with two different instances of the same class, not two references to the same object.


The Tricky Edge Case: Object References vs Primitives

This is the interview trap most candidates walk straight into. When you assign an object to a variable, you're not copying the object. You're copying a reference to it. Two variables can point to the same object in memory.

class Config {
  constructor(theme, language) {
    this.theme = theme;
    this.language = language;
  }
}

// Trap: assigning an object does NOT create a copy
const configA = new Config("dark", "en");
const configB = configA;              // configB points to the SAME object

configB.theme = "light";

console.log(configA.theme); // "light" — configA was also changed!
console.log(configB.theme); // "light"
console.log(configA === configB); // true — same object in memory

// Fix: create a genuinely new object instead
const configC = new Config(configA.theme, configA.language);
configC.theme = "dark";

console.log(configA.theme); // "light" — configA is unaffected now
console.log(configC.theme); // "dark"
console.log(configA === configC); // false — different objects

configA and configB are the same object. Changing one changes both because they are two names for the same thing in memory. This is the reference vs value distinction that JavaScript interviews test repeatedly.

[ORIGINAL DATA]: In technical screening sessions, candidates who have been writing JavaScript for two or more years still fail this specific scenario at a measurable rate when it is embedded in a slightly larger code block. The fix is always the same: construct a new object explicitly rather than assigning an existing reference.

Class & Object: The Blueprint and the Thing You Actually Build visual 2


Common Mistakes

  • Forgetting new: Calling Car("Toyota", "Camry", 2022) without new does not create an object. In strict mode it throws a TypeError. Outside strict mode, this inside the constructor refers to the global object, silently polluting it with unexpected properties. Always use new.

  • Treating assignment as copying: Writing const b = a when a is an object gives you two references to one object. This catches developers off guard when they modify b and see a change. If you need an independent copy, construct a new instance explicitly.

  • Putting shared state in the constructor with a reference type: Writing this.items = someExternalArray means all instances pointing to that same external array will share mutations. Always initialize arrays and objects fresh inside the constructor: this.items = [].


Interview Questions

Q: What is the difference between a class and an object?

A class is a blueprint or template that defines structure and behavior. An object is a concrete instance created from that class at runtime. The class itself is never in memory as a live value at runtime — only objects are.

Q: What does the constructor do, and when does it run?

The constructor is a special method that runs exactly once, immediately when new ClassName(...) is called. Its job is to initialize the new object's properties with starting values. If you don't define a constructor, JavaScript provides an empty default one.

Q: Can you have multiple objects from one class, and are they independent?

Yes. You can create as many instances as needed. Each object gets its own copy of the instance properties set in the constructor. Changing a property on one object does not affect any other object, because each lives at its own memory address.

Q: What does instanceof check?

instanceof checks whether an object was created from a specific class (or has that class's prototype in its prototype chain). car1 instanceof Car returns true. typeof car1 returns "object" — it does not return "Car".

Q: If two variables hold the same object and you change a property through one variable, what happens to the other?

Both variables reflect the change. In JavaScript, objects are assigned by reference, not by value. When you write const b = a, both a and b point to the same underlying object in memory. A true independent copy requires constructing a new instance.


Quick Reference — Cheat Sheet

CLASS vs OBJECT
---------------------------------------------------------------
Class                       | Object (Instance)
---------------------------------------------------------------
Blueprint / template        | The actual thing in memory
Defined once                | Created as many times as needed
Not in memory at runtime    | Lives at a specific memory address
Defines structure/behavior  | Holds real data values
typeof SomeClass -> "function" | typeof someObj -> "object"
---------------------------------------------------------------

CLASS ANATOMY
---------------------------------------------------------------
class ClassName {
  constructor(param) {      <- runs once on `new`, sets state
    this.property = param;  <- instance property
  }
  methodName() { ... }      <- shared via prototype (not copied)
}

CREATING INSTANCES
---------------------------------------------------------------
const obj = new ClassName(args);   <- correct
const obj = ClassName(args);       <- wrong, throws or pollutes global

KEY OPERATORS
---------------------------------------------------------------
obj instanceof ClassName   -> true / false
typeof obj                 -> "object" (always, for class instances)
obj1 === obj2              -> true only if same object in memory

REFERENCE TRAP
---------------------------------------------------------------
const a = new Config();
const b = a;              // b and a point to the SAME object
b.theme = "dark";         // a.theme is now "dark" too

const c = new Config();   // c is a NEW, independent object

Previous: Lesson 1.1 - What is OOP? → Next: Lesson 1.3 - The 4 Pillars Overview →

This is Lesson 1.2 of the OOP Interview Prep Course — 8 chapters, 41 lessons.

On this page