"owns, but does not own the LIFE of"
The warden works at the hostel — but has a family, a house, a life outside it. The hostel merely holds a reference to an independent person.
Class 2 · One sealed box — class = blueprint
This session runs two ways. Pick the one that matches where you're sitting — you can switch any time from the top bar.
Part A · Class 2 of 2 · The sealed-box blueprint — and your first Java file
Class 1 ended with a promise: keep related facts and the steps that use them in one place, behind one door. Today that idea gets its real names — class and object — and by the end of the hour a main method you typed yourself prints its first line on your own machine.
HelloWorld.java on your own laptop — javac then java, no IDE.11 TOPICS · 4 BUILD-UPS · 1 CODE FILE · 1 ACTIVITY · 3 HOMEWORK
PART 2 · THE PICTURE THAT FIXES CLASS 1
Class 1's register failed because facts lived in four notebooks and the rules lived in four heads. The fix is a single box that holds both — and lets nobody reach inside.
BUILD-UP · THE SEALED BOX, ONE PIECE PER PRESS
You don't reach into the box. You knock on the door. ✦
Rohit can no longer scribble in Diya's notebook. If he wants ₹120 of chai added, he must call addExpense(120) — and the box updates its own total, correctly, every single time. That discipline — data and the methods that guard it, sealed together — is the whole of object-oriented thinking. Everything else in this course is detail.
PART 3 · NAMING THE PICTURE
One box design can serve every flat in Hyderabad. The design is drawn once; real boxes are stamped from it as many times as needed.
BUILD-UP · ONE BLUEPRINT, THREE STAMPS
Says what facts every register holds and what operations every register offers. Holds no rupees itself — a drawing of a box is not a box.
▼ STAMPED FROM THE SAME BLUEPRINT ▼
kondapurFlat
month: "August"
total: ₹18,340
gachibowliPG
month: "August"
total: ₹9,760
madhapurHostel
month: "July"
total: ₹26,105
THE BLUEPRINT · EXISTS ONCE
A description: which facts, which operations. Written by the programmer, stored in a .java file. It spends no money and holds no month — it only says what registers look like.
THE STAMPED THING · EXISTS MANY TIMES
A real box in memory with its own copies of the facts. kondapurFlat and gachibowliPG came from one blueprint but hold different totals — change one, the other doesn't blink.
And "instance"? Same thing as object — just pointing at its parent. We say kondapurFlat is an instance of ExpenseRegister, the way you'd say "this samosa is an instance of the canteen's samosa recipe". Object emphasises the thing; instance emphasises which class stamped it. One idea, two spotlights.
PUT A NUMBER ON "DRAWN ONCE" — THE BLUEPRINT ECONOMY
Say the register blueprint takes 200 lines of careful code to write, and every flat that adopts it fills in just 2 lines of its own facts (name + month). Compare the two worlds as Hyderabad scales up:
| SCALE | FLATS | EVERYONE WRITES THEIR OWN | ONE BLUEPRINT, MANY STAMPS |
|---|---|---|---|
| Our building | 10 | 10 × 200 = 2,000 lines | 200 + 20 = 220 lines — 9× less |
| Kondapur | 1,000 | 1,000 × 200 = 2,00,000 | 200 + 2,000 = 2,200 — 90× less |
| All of Hyderabad | 10,00,000 | 20 crore lines | 200 + 20 lakh ≈ 20 lakh — 100× less |
And the kicker isn't even the typing — it's the FIXING. A bug in the blueprint world is repaired once, in 1 place. In the everyone-writes-their-own world the same bug hides in 10,00,000 places. That single column is why the word "class" exists.
PART 4 · WHY THIS LANGUAGE
Many languages can draw sealed boxes. We pick Java for three reasons — the full James Gosling story, oak trees and all, waits until Class 7 when you've earned it.
In Java you cannot write code outside a class. The sealed-box discipline isn't an optional style — the language enforces it on line one of every file.
No stray pointers, no manual memory freeing, and mistakes are caught at compile time — before your program ever runs, not after it crashes.
One .class file runs on Windows, Mac and Linux unchanged. We use JDK 21 — the current Long-Term-Support release the industry ships on.
Who made it, and why the coffee cup? That's a genuinely good story — a frustrated team, set-top boxes, and a tree outside a window. It deserves more than a footnote, so it's parked at Class 7, right after your first lab. Hold the question.
PART 5 · TOOL-UP
Do these at home if the class Wi-Fi sulks. No IDE this semester's start — Notepad++ and the command prompt keep every moving part visible. Step 5 is the fun one: you'll make Java answer you before writing a single file.
Go to oracle.com/java, open Downloads, pick JDK 21 LTS, then the Windows x64 Installer. Run it, keep clicking Next, accept the default folder.
Open a new Command Prompt window and ask both tools to introduce themselves:
Go to notepad-plus-plus.org/downloads, click the topmost release, and download the Installer, 64-bit x64 for Windows. Run it: keep English, click Next on every wizard screen, accept the default folder C:\Program Files\Notepad++, click Install, then Finish. A green chameleon icon lands in your Start menu — that is the whole install.
Open Notepad++, then menu Language, submenu J, click Java. Keywords turn blue, strings turn red — the editor now reads Java with you.
The JDK ships a scratchpad called JShell (the one from Class 0's toolchain): type any Java expression, get the answer instantly — no file, no class, no compile. Try these at home:
Yes — 7 / 2 gave 3, not 3.5. Park that surprise; Class 3 explains it, and Lab 0 examines it.
If java -version works but javac -version says "not recognized", you installed the JRE (runner) without the JDK (builder) — re-run the JDK 21 installer. We need both: javac builds, java runs.
PART 6 · FIRST FILE
Watch it grow one line per press on the left; the command prompt on the right shows exactly what the class will see. Note the brace discipline — every { and every } stands on its own line (Allman style). Type along — do not copy-paste.
HelloWorld, saved as HelloWorld.java in class-02 — name and file must match, capital H and W.main method — the exact eight-line skeleton shown below, Allman braces.Hello CSE-B!Hello CSE-B! — one line, no errors, after javac HelloWorld.java then java HelloWorld.C:\Users\diya\Desktop\java-practice\class-02> javac HelloWorld.java
C:\Users\diya\Desktop\java-practice\class-02> java HelloWorld
Hello CSE-B!
No errors. One line of output. Your machine just ran a program you wrote. ✦
THE FULL ROUND TRIP — SAVE, COMPILE, RUN
helloworld.java is not HelloWorld.java. The public class name must match the file name exactly — one wrong capital and javac refuses politely but firmly. This is the #1 first-week error; earn the habit today.
PART 7 · THE LINE EVERY PROGRAM NEEDS
Line 4 of HelloWorld is where every Java program begins. Today you name each of its five pieces — each numbered piece maps to the numbered card below it — and hold one honest line about each; three of the five get their full chapters later.
BUILD-UP · ONE PIECE PER PRESS — THE NUMBER ON EACH PIECE POINTS TO ITS MATCHING CARD BELOW
Visible from outside the box. The JVM lives outside your class — it must be allowed to knock on this door.
FWD C13 · ACCESS MODIFIERSBelongs to the blueprint itself, not to any stamped object — so it can run before a single object exists.
FWD C9 · STATIC MEMBERSHands nothing back when it finishes. main does its work and ends — there is nobody above it waiting for a return value.
The agreed name. The JVM looks for exactly this word — call it Main or start and your program compiles but never begins.
A tray for words handed in from the command line. Empty today — it starts catching real input very soon.
FWD C3 · INPUT & DATA TYPESMemorise the music, not just the words: public static void main — String args in a tray. Say it twice. In the exam hall, the five pieces written in order with one line each is a full-mark answer waiting to happen.
PART 8 · YOUR TURN
Diya has typed everything except one statement. Line 8 is yours: print her roll number so the output matches the target card exactly — spacing included. Braces stay Allman — every { and } on its own line.
AboutMe, saved as AboutMe.java in class-02.Roll : 24CS045 — capital R, one space either side of the colon.Name : Diya · Class: CSE-B · Roll : 24CS045.TARGET OUTPUT — MATCH IT EXACTLY
Name : Diya Class: CSE-B Roll : 24CS045
RULED NOTEBOOK FIRST — WRITE LINE 8 BY HAND
Write the full statement in your notebook — capitals, dots, quotes, semicolon. Look at line 6 and line 7 for the shape. Then, and only then, unlock the sheet below.
Notebook first — the hand remembers what the eye forgets.
C:\Users\diya\Desktop\java-practice\class-02> java AboutMe
Name : Diya
Class: CSE-B
Roll : 24CS045
Missing semicolon: the compiler stops you. Wrong spacing inside the quotes: it compiles fine but the output is wrong. Two different kinds of mistake — Java only catches the first kind for you.
PART 9 · HOW BOXES RELATE
Boxes don't live alone. Two — and only two — relationships connect them, and plain English already tells you which is which.
A DELIVERY PARTNER IS-A PERSON
Everything true of a person — name, phone number — is automatically true of a delivery partner. The special box inherits everything the general box has, then adds its own.
A DELIVERY PARTNER HAS-A BIKE
The bike is a separate box kept inside — its own facts (fuel, number plate), its own operations. The partner uses the bike; the partner is not a kind of bike.
"A car IS-A engine" — tempting, wrong. A car HAS-A engine. Say the sentence aloud: if "X is a kind of Y" sounds silly, it's HAS-A. The sentence test scores marks; guessing loses them.
LIGHTNING DRILL — SHOUT THE ANSWER BEFORE THE STAMP LANDS · 6 ROUNDS
"Biryani is a kind of dish" — sounds right, IS right.
"A laptop is a kind of battery" — silly. The battery is a separate box kept inside.
The app owns and uses a scanner. It is not a kind of scanner.
Every electric scooter is a vehicle — everything true of vehicles comes free.
Trap round — a hostel HAS a warden, and the warden is not "part of the building's kind". Ownership, not kinship.
The ladder said FoldablePhone IS-A Smartphone. But the hinge? "A phone is a kind of hinge" — no. The hinge is a component. Same object, both relationships, different partners.
Score yourself: 6/6 and the sentence test is now a reflex — which is exactly what the 2-mark question wants. Anything less: re-run the drill, it costs 60 seconds.
PART 10 · CLIMBING UP
Chain IS-A links and a ladder appears. Krish's new folding phone climbs it in two steps — each rung up drops detail and keeps what's shared.
BUILD-UP · CLIMB ONE RUNG PER PRESS (BOTTOM TO TOP)
The most general rung in our story — anything with power and a purpose. Above this, one root rules them all (next part).
▲ IS-A · every smartphone is a gadget
Drops the hinge detail; keeps calls, camera, apps — everything all smartphones share.
▲ IS-A · every foldable is a smartphone
Krish's actual phone — most specific: it folds, it creases, it cost too much.
SECOND LADDER · DIFFERENT DOMAIN, SAME MOVE — PROVE IT'S A WAY OF SEEING
Anything the mess can serve — a name, a price, "is it veg?". The drill's round 1 lives here.
▲ IS-A · every biryani is a dish
Drops "which meat/veg" detail; keeps what all biryanis share — rice, dum, the fight over raita.
▲ IS-A · every chicken biryani is a biryani
Sunday's actual order — most specific: chicken, extra leg piece, ₹280 at the mess canteen.
Phones climbed one ladder, food climbs another — the move is identical. Generalisation/specialisation is not a phone trick or a food trick; it is how OOP sees ANY domain. Exam asks "give an example"? You now own two.
PART 11 · THE TOP OF EVERY LADDER
Keep climbing any ladder in Java and you always arrive at the same rung. This is a preview — plant the picture, details come with inheritance.
ExpenseRegister, FoldablePhone, HelloWorld — every class you will ever write hangs somewhere on one shared type hierarchy. No class floats alone.
The topmost rung is a built-in class literally named Object — the most general box possible. Say nothing else about your class, and Java still says: it IS-An Object.
Because every object shares a common ancestor, Java can promise a few abilities to all of them — comparing, describing, storing together. That promise powers half of what's ahead.
PART 12 · HAS-A, SHARPENED
The drill stamped six HAS-A verdicts as if they were all the same. They are not. OOP splits "owns a" into two flavours — and there is one destruction test that separates them every time.
"owns, but does not own the LIFE of"
The warden works at the hostel — but has a family, a house, a life outside it. The hostel merely holds a reference to an independent person.
"owns, AND owns the life of"
Room 204 exists only as part of that building. It cannot pack up and join another hostel; the whole creates it and the whole takes it down.
Both are HAS-A. Aggregation: the part can exist without the whole (Hostel–Warden, Laptop–Pendrive, Team–Player). Composition: the part cannot (Hostel–Room, Laptop–Motherboard, Human–Heart). Ask "if I destroy the whole, does the part survive?" — that single question scores the mark.
DESTRUCTION-TEST DRILL · 4 ROUNDS — SHOUT BEFORE THE STAMP
Scrap the car — that engine block dies with it (in our design, it was built into the chassis). Whole owns the part's life.
Scrap the car — the driver walks away and drives another one. Independent life means loose ownership.
Delete the register — the entries are gone with it. They never existed anywhere else. Our Part-2 box is a composition.
Delete the register — Diya, Rohit, Krish and Trish are all still very much alive (and still owe money). The register only refers to them.
MESSAGE PASSING · HOW SEALED BOXES TALK
If nobody may reach into a box, how does anything happen? Objects send each other messages — a request through the door, never a hand through the wall. Watch one rupee travel through three objects:
ONE PRESS = ONE MESSAGE · ROHIT PAYS THE WIFI BILL
Exam line, memorise: message passing = objects communicating by invoking each other's methods — request by name, hand over the data, get a reply. A program is objects passing messages, not lines poking at shared facts.
PART 13 · JUDGING A DESIGN
You can now build boxes and connect them. But which designs are GOOD? Engineers judge every design with two dials — one measured inside a box, one measured between boxes.
"DOES EVERYTHING IN HERE BELONG TOGETHER?"
High cohesion = good. ExpenseRegister holds expenses and expense operations — one job, done fully. Now imagine it also stored Rohit's photos and cricket scores: same box, three unrelated jobs — LOW cohesion, a junk drawer.
"HOW HARD DOES A CHANGE HERE HIT THE OTHERS?"
Low coupling = good. Rohit knows only the register's door: addExpense(…). If the register's insides change, Rohit's code doesn't. If he depended on HOW the total was stored — TIGHT coupling — every internal change would break him.
Inside a box: tightly related (high cohesion). Between boxes: barely acquainted (low coupling). Students reverse it under exam pressure — anchor it to the flat: the register does ONE job, and flatmates talk to it ONLY through the slot.
PUT A NUMBER ON COUPLING — CLASS-1'S EXPLOSION, ONE LAST TIME
Every box wired to every other: n(n−1)/2 = 4×3/2 = 6 connections. Change one notebook's format and up to 3 people's routines break. At 100 modules: 4,950 wires — the software crisis table, rediscovered as a COUPLING disaster.
One wire per person: n = 4 connections. Change the register's insides and 0 flatmates break — the door stayed the same. At 100 modules: 100 wires, not 4,950 — a 49× reduction, from one design decision.
Same series, third appearance — scattered notebooks (Class 1), pair-reconciliation (Class 1), and now coupling. One formula, n(n−1)/2, explains why every OOP rule in this class exists. That is not a coincidence; that is the syllabus.
Your full relationship toolkit, one breath: IS-A (inheritance — biryani is a dish) · HAS-A loose (aggregation — hostel has a warden) · HAS-A strict (composition — hostel has Room 204) · boxes cooperate by message passing (requests through doors) · and you judge the result by high cohesion, low coupling. Five terms, five flat-and-hostel pictures — the whole 2-mark section of Unit 1, done.
Textbooks open with four grand words. You built each one with your hands this hour — here is the map from today's pictures to tomorrow's vocabulary. Ten minutes, before the next class.
The sealed box itself — facts and operations together, one door, no reaching in. Part 2 was encapsulation before you knew its name.
The IS-A ladder — FoldablePhone getting everything Smartphone has for free. Chapters of this from Unit II onward.
One door, many behaviours — printSummary() answering differently for a flat than for a hostel. The most exam-loved word of the four.
The blueprint hiding the wiring — Rohit calls addExpense(120) and never sees how the total updates. Show the door, hide the machinery.
Exam-quotable, one line each — copy these into your notes today: Encapsulation — binding data and the methods that operate on it into a single unit, hiding the data from direct outside access. Inheritance — a mechanism by which one class acquires the fields and methods of another (IS-A). Polymorphism — one interface, many implementations; the same call behaving differently on different objects. Abstraction — exposing only essential features while hiding implementation detail.
Cover the cards above and answer from memory: ① Which pillar is the sealed box from Part 2? ② Which pillar is the IS-A ladder? ③ printSummary() answering differently for a flat and a hostel — which word? ④ "Show the door, hide the machinery" — which word? If any answer takes more than five seconds, re-read that one card. Class 3 uses all four words without stopping to re-explain them.
PART 15 · BEFORE YOU GO
If your machine matches this tree, today succeeded. Two files you wrote, two files javac made for you.
HOMEWORK · DUE BEFORE CLASS 3
ThreeLines.java
Three println lines about your day — any three facts. Full round trip: save, javac, java. Bring the output on your phone camera if it worked, the error message if it didn't. Both are welcome.
LibraryBook
No code — ruled notebook only. Draw the sealed box for a library book: at least 3 facts, at least 3 operations, one door. Ask yourself: who is allowed to change dueDate?
Part 12 above, ten minutes. Class 3 opens by asking which pillar the sealed box belongs to — first correct hand skips the warm-up question.
Homework 1 and 2 solved below — your own run/sketch first, then compare.
C:\Users\diya\Desktop\java-practice\class-02> javac ThreeLines.java
C:\Users\diya\Desktop\java-practice\class-02> java ThreeLines
Woke up at 6 for the bus.
Mess served upma again.
Compiled my first Java file!
Silence after javac = success. Your three facts will differ — the shape must not: class name matches the filename, Allman braces, semicolon on every println.
HW 2 · LibraryBook blueprint — the model sketch. One sealed box labelled LibraryBook. FACTS inside: title, borrowerName, dueDate (3 minimum — ISBN, shelfCode also fine). OPERATIONS on the wall: borrow(), returnBook(), extendDueDate(). One DOOR through which every request passes.
Only the box itself, through extendDueDate() — never outside code reaching in. If your sketch lets anyone scribble on dueDate directly, the box isn't sealed; that one sentence is exactly the pillar Class 3's warm-up asks about.