🏗️ OOP & Collections

OOP & Collections: In OOP a class bundles the data (fields) and what can be done with that data (methods) into a single capsule — just as a LEGO piece carries both its shape (dat

In OOP a class bundles the data (fields) and what can be done with that data (methods) into a single capsule — just as a LEGO piece carries both its shape (data) and the studs that dictate where it can attach (behavior) in one part: you design the piece once, then combine it thousands of times safely because each piece carries its own rules inside. But if you could keep everything in one giant function, why bother splitting data into classes and dealing with encapsulation? Because in one giant block you cannot tell where changing a field will break things; classes isolate responsibility so a change affects only the relevant piece. In Java, Collections (ArrayList, HashMap, HashSet) are the type-safe boxes where you store these pieces; unlike Python's loose list/dict, Java's `List ` prevents you from adding the wrong type at compile time. For a QA engineer this isolation is priceless: the Page Object Model is exactly OOP — each page becomes a class, and when a locator changes you update only that class instead of hand-fixing 200 test files, preventing maintenance-driven broken tests.

Class → Object → Usage

Passive property = field, active behavior = method. QA Page Object models use the same idea.

Class definition and Object creation

Micro Lab: Code practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

What Happens in Memory When new TestUser(...) Is Called?

public class TestUser is only a BLUEPRINT…

`public class TestUser` is only a BLUEPRINT — by itself it allocates no memory, just as a cookie cutter is not a cookie.

THE MOMENT new TestUser("admin"…

THE MOMENT `new TestUser("admin", "admin@test.com", 30)` is called, the JVM allocates a new area on the heap and the constructor fills it.

The user1 variable is not the object ITSELF…

The `user1` variable is not the object ITSELF, it is a REFERENCE pointing to that heap area — `user2` is a different reference pointing to a different memory area.

When System.out.println(user1) is called…