← Java OOP · guided lesson
OOP for DSA & Design Rounds
the story
Classes, objects & encapsulation
Every tool here exists because something was too slow or broke without it. Here is what.
Dahl and Nygaard, at the Norwegian Computing Center, wanted to describe and simulate real systems. Simula I, a simulation language, and Simula 67, a general-purpose language built on ALGOL 60, introduced classes, objects and inheritance. In 2001 they received the ACM Turing Award "for ideas fundamental to the emergence of object oriented programming". Java's class, new and extends are those same three ideas.
the problem it solved
Keep each piece of state together with the only code allowed to change it, and create as many independent copies (objects) as the program needs.
why it matters: without it
State and the code that guards it drift apart: any line could set a count to −1, and design answers become bags of global variables.
sources: ACM A.M. Turing Award 2001: Kristen Nygaard ↗ · ACM A.M. Turing Award 2001: Ole-Johan Dahl ↗
also in this chapter: Static nested vs inner classes · The equals/hashCode contract · Records · Generics · Enums
the idea
The idea in 30 seconds
A class is a blueprint; new builds an object from it on the heap. Every linked list and tree you write is objects pointing at objects.
Design questions ("Design a HashSet", "Design a Parking System") test one habit: keep the state in private fields, and make each operation a small method that checks, updates and returns.
HashMap, HashSet and sorting trust your class's equals, hashCode and compareTo. Keep those contracts and the library does the heavy lifting.
picture this
A Google Form: the form (class) defines the questions once, and every submission (object) holds its own answers. A setting printed on the form itself, like the closing date, is shared by every submission: that is a static field.
watch it
Design Linked List: Node objects and next references
Predict mode is on. Before each step, guess what happens, then watch. Wrong guesses are fine; that is how it sinks in.
Design Linked List: Node objects and next references
The MyLinkedList object holds size and an arrow to a dummy node; each Node holds a value and a next arrow. An insert is two arrow changes, in the right order.
On this screen ← / → step the simulation. Use the buttons below to move through the lesson.
watch it
Design HashSet: hashCode picks the bucket, equality confirms
Predict mode is on. Before each step, guess what happens, then watch. Wrong guesses are fine; that is how it sinks in.
Design HashSet: hashCode picks the bucket, equality confirms
Each call searches one bucket only. With 5 buckets, keys 1, 6, 11 and 16 all land in bucket 1: a collision chain.
On this screen ← / → step the simulation. Use the buttons below to move through the lesson.
key idea 1 of 10
Read it once. Say it out loud once. Bas.
Class vs object: a field without static exists once per object; a static field exists once per class, shared by every object.
key idea 2 of 10
Read it once. Say it out loud once. Bas.
Constructors build a valid object: this.size = size copies the parameter into the field, this(...) reuses another constructor, and final fields are set exactly once.
key idea 3 of 10
Read it once. Say it out loud once. Bas.
Encapsulation: private fields, changed only through methods that check their input. In design rounds, invalid calls return false or -1 exactly as the statement says.
key idea 4 of 10
Read it once. Say it out loud once. Bas.
`private static class Node` inside your class: no hidden link to an outer object, so nodes stay small and can be created from anywhere. Drop static only when the nested object must read the outer object's fields (an iterator).
key idea 5 of 10
Read it once. Say it out loud once. Bas.
LeetCode's `ListNode` / `TreeNode` are plain classes with fields val, next, left, right and three constructors. They are already defined for you: never redeclare them.
key idea 6 of 10
Read it once. Say it out loud once. Bas.
Interfaces say what (Deque, Map, Comparable, Iterator); classes say how. Declare by interface: Deque<Integer> st = new ArrayDeque<>();.
key idea 7 of 10
Read it once. Say it out loud once. Bas.
equals/hashCode contract: equal objects must have equal hash codes. A HashMap uses hashCode to pick the bucket and equals to confirm. Override both or neither.
key idea 8 of 10
Read it once. Say it out loud once. Bas.
Records (Java 16+) give a constructor, accessors, equals, hashCode and toString in one line: ideal for pairs, grid cells and HashMap keys.
key idea 9 of 10
Read it once. Say it out loud once. Bas.
Generics (List<T>, <T extends Comparable<T>>) write code once for every object type. Enums give named, type-safe constants (directions, states) that can carry data.
key idea 10 of 10
Read it once. Say it out loud once. Bas.
Ordering objects with Comparable and Comparator is taught in the Java Toolkit chapter.
spot the pattern
When you see this, think this
"Design" or "Implement the X class" with a list of methods
Private fields for state, a constructor, one small checked method per operation
You need a Node, TrieNode or Entry inside your class
private static class NodeA pair or grid cell must be a HashMap key or go in a HashSet
A record, or equals + hashCode written together
Four directions, three states, a fixed set of types
An enum (or one constant
int[][]table)
+2 more signals on the chapter page
watch out
A trap or two
Many wrong answers in interviews come from small slips like these, not from the hard part.
- Declaring
class Node(nostatic) inside Solution and callingnew Node()from a static method: compile error. - Using
staticfields for per-object state in a design problem: two objects share one counter.
quick check
One question. No pressure.
class Lot { static int count; int free; } Two Lot objects are created, and each runs count++; free++;. What does the first object see?
now try
Your first real problem
You know enough to try this one. Stuck? Use the hints; that is what they are for.
Design HashSet
open the problem →recall
Flip a few cards
Answer in your head first, then flip. Grade yourself honestly; the schedule brings back what you forget.
Why private static class Node rather than private class Node inside Solution?
Question 1: Why private static class Node rather than private class Node inside Solution?
done
Lesson complete
Chalo, that is the core. The full chapter has the rest: cost tables, Java details and more problems.