Cosmic Module

J

Qubits of DPK

March 14, 2026

Core Java

Layman Explanation

A class is a blueprint. An object is the real thing built from that blueprint. Blueprint of a house → Class. Actual house built from it → Object.

What a Class Contains

javascript
QUBITS OF DPK
1Class
23├── Fields (Variables)WHAT it HAS (color, speed, brand)
4└── Methods (Functions)WHAT it DOES (accelerate, brake, honk)

Code Example

java
QUBITS OF DPK
1class Car {
2    // Fields
3    String brand;
4    String color;
5    int speed;
6
7    // Method
8    void accelerate() {
9        speed += 10;
10        System.out.println(brand + " speed: " + speed);
11    }
12}
13
14class Main {
15    public static void main(String[] args) {
16        Car myCar = new Car();    // Object created
17        myCar.brand = "Toyota";
18        myCar.speed = 0;
19        myCar.accelerate();       // Toyota speed: 10
20    }
21}

Memory Model

javascript
QUBITS OF DPK
1Stack                    Heap
2────────────────         ─────────────────────────
3myCar (reference) ─────► Car Object:
4                         brand = "Toyota"
5                         color = null
6                         speed = 10

Reference Sharing Trap

java
QUBITS OF DPK
1Car car1 = new Car();
2car1.speed = 100;
3
4Car car2 = car1;        // SAME object!
5car2.speed = 200;
6
7System.out.println(car1.speed);  // 200 — same heap object!

this Keyword

java
QUBITS OF DPK
1void setSpeed(int speed) {
2    this.speed = speed;   // this.speed = field, speed = parameter
3}
4// Without this → parameter assigned to itself, field unchanged!

️ All Traps

java
QUBITS OF DPK
1// Trap 1 — NullPointerException
2Car car = null;
3car.accelerate();           // ❌ NPE
4
5// Trap 2 — Reference sharing
6Car a = new Car();
7Car b = a;                  // same object!
8b.speed = 100;
9System.out.println(a.speed); // 100!
10
11// Trap 3 — this keyword
12void set(int speed) {
13    speed = speed;           // ❌ field not updated!
14    this.speed = speed;      // ✅
15}
16
17// Trap 4 — Class has no memory until 'new'
18Car c;
19c.brand = "BMW";            // ❌ NullPointerException
20c = new Car();
21c.brand = "BMW";            // ✅

Interview Answer (SDE-2)

"A class is a blueprint defining fields (properties) and methods (behaviors). An object is a real instance created with 'new' keyword — reference stored in stack, actual object in heap. Multiple objects from one class each get independent copies of instance fields. 'this' keyword refers to the current object — used to disambiguate between field and parameter with same name. Key trap: two references pointing to same object means modification via one reflects in the other — called reference sharing, fundamental to linked lists and trees in DSA."

Interview Questions & MAANG-Level Answers

Q1. What is the difference between class and object?
A class is a blueprint/template defining fields and methods — it has no memory allocated. An object is a real instance created from the class using new — memory is allocated in heap. Multiple objects can be created from one class, each with independent field values. Analogy: Class = house blueprint (one). Objects = actual houses built from it (many).
Q2. Where is the object stored — stack or heap?
The object is stored in Heap memory. The reference variable is stored in Stack. Example: Car myCar = new Car()myCar (the reference/address) lives in stack, the actual Car object lives in heap. This is why when you pass an object to a method, you pass the reference (stack value), and both the caller and callee can modify the same heap object.
Q3. What is the this keyword used for?
this refers to the current object instance. Five uses: (1) disambiguate field vs parameter with same name: this.speed = speed, (2) call another constructor: this("BMW", 100) (must be first line), (3) pass current object to a method: engine.attach(this), (4) return current object for method chaining (Builder pattern): return this, (5) access outer class from inner class. Cannot use this in static methods since static has no object context.
Q4. What happens when two references point to the same object?
Both see the same data and any modification through one is visible through the other:
java
QUBITS OF DPK
1Car a = new Car(); a.speed = 0;
2Car b = a;          // same heap object
3b.speed = 100;
4System.out.println(a.speed); // 100!
This is called reference sharing. Critical in DSA: LinkedList nodes, Tree nodes, and Graph nodes all use this. Setting a = null does NOT destroy the object if b still points to it.
Q5. What is NullPointerException and when does it occur?
NPE is thrown when you try to use a null reference — call a method, access a field, or use it as an array. String s = null; s.length() throws NPE. Common causes: uninitialized object reference, method returning null not checked, array of objects before initializing elements. Prevention: null checks before use, Optional<T> in Java 8+, @NonNull annotations in frameworks like Spring.