Blueprints & Objects
How does a computer distinguish between two 'Student' objects in memory? Explore the separation of Blueprints and Instances.
In our journey through LLD, we often use the word "Blueprint" for a class. But what happens when that blueprint is actually built into a real house? How does the computer manage ten different houses built from the same plan?
To understand this, we need to look at Memory Isolation.
The Essentials
The "Memory Map" for this post:
- Class = Idea: The class is the blueprint. It takes up almost no space and cannot "do" anything on its own.
- Object = Body: The object (instance) is the actual physical entity in the computer's RAM.
- Independence: Every object has its own unique space. Changing "Student A" has zero effect on "Student B."
- The Dot Operator: We use
.to enter an object's world and access its internal attributes.
The Blueprint Analogy
Imagine you are an architect. You draw a beautiful plan for a house.
- Is that plan a house? No. It is just an Idea. In programming, we call this the Class.
- Is the physical building the plan? No. It has a physical address and bricks. In programming, we call this an Object or an Instance.
When a builder takes that plan and uses bricks to build it, we get a House.
Blueprints in Action: The Memory Problem
The class is just a set of instructions. You can't live in a blueprint; it has no physical space in memory.
name: string;
login() { .. }
}
It defines "How" things will look, but it doesn't "exist" until you build it.
Each object has its own unique address in RAM. Changing "Denver" doesn't touch "James" because they are isolated.
s1.name = "Denver";
let s2 = new Student();
s2.name = "James";
Each "Physical Body" is independent. This is the secret to managing thousands of users at once.
Code Implementation: Creating Real Bodies
Here is how you instantiate multiple objects from a single blueprint and prove they are isolated:
class Student {
name: string;
batch: string = "Aug-2024";
}
// Instantiate two distinct objects
const s1 = new Student();
s1.name = "Denver";
const s2 = new Student();
s2.name = "James";
s2.batch = "Sept-2024";
console.log(s1.name); // "Denver"
console.log(s1.batch); // "Aug-2024" (Untouched by s2!)In the computer's RAM (memory), two separate "boxes" are created. Because they have different physical addresses, they are independent. If Denver changes his batch, James's batch stays the same.
The Dot Operator: Your Key to the World
To interact with an object, we use the Dot Operator (.).
By building your software around Ideas and Abstraction, you ensure that no matter how complex the system becomes, it remains understandable, maintainable, and human.
In the next post, we'll explore Access Modifiers in depth and learn how to hide the "internal circuits" of our objects.
Practice what you just read.