Java Notes
Object-Oriented Programming in Java — what OOP means, why it exists, the four pillars (encapsulation, abstraction, inheritance, polymorphism), and how a class and object work together.
Before OOP existed, programs were written as one long list of instructions — one thing after another. That worked fine for small programs. But as programs grew larger, everything became connected to everything else. Change one variable in one place, something breaks somewhere else. Nobody could keep track.
OOP was invented to solve this. The idea: organise a program around objects, not instructions. An object holds its own data and knows how to work with it. Different parts of a program only see what they need to see. Changes stay contained.
Java is built entirely around this idea. Every Java program has at least one class.
What is an Object?
In real life, everything is an object — a phone, a student, a bank account. Each object has two things:
- State — what it knows (a phone has a battery level, a student has a name and marks)
- Behaviour — what it can do (a phone can make a call, a student can get a result)
In Java, an object is the same — it holds data (called fields) and provides actions (called methods).
What is a Class?
A class is the blueprint for creating objects. It defines what fields and methods every object of that type will have. The class itself is not an object — it is just the plan.
One class can create many objects, each with their own data.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
class Student— defines the blueprint.String name; int marks;— every Student object will have these two fields.void printResult()— the behaviour; each object calls this on its own data.Student s1 = new Student();— creates one actual object in memory.s1.name = "Asha";— sets data on that specific object.s2is a completely separate object — changings2.marksdoes not affects1.
Run this and you will see each student's own result printed.
The Four Pillars of OOP
These are the four ideas that make OOP powerful. You will study each one in depth in its own lesson. Here is the big picture:
Encapsulation
Keep an object's data private and control access through methods. This prevents other code from accidentally (or intentionally) putting wrong values in.
public class BankAccount {
private double balance; // private — nobody outside can touch this directly
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}balance is hidden. You cannot write account.balance = -9999 from outside. The only way to change it is through deposit(), which checks the amount first.
Why it matters: your data stays valid. No random code can corrupt it.
Abstraction
Show only what is necessary. Hide the internal complexity.
public class WashingMachine {
public void startWash() {
fillWater();
addDetergent();
rotateDrum();
drain();
spin();
}
private void fillWater() { /* internal detail */ }
private void addDetergent() { /* internal detail */ }
private void rotateDrum() { /* internal detail */ }
private void drain() { /* internal detail */ }
private void spin() { /* internal detail */ }
}You call startWash(). You do not need to know about rotateDrum(). The machine handles it. That is abstraction — a simple interface hiding a complex operation.
Why it matters: code becomes easier to use, and the internal details can change without breaking anything that calls startWash().
Inheritance
Let one class reuse the fields and methods of another class. This models a real-world "is-a" relationship.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Dog extends Animal — it gets breathe() for free. A Dog is an Animal, so this makes logical sense.
Why it matters: write common behaviour once in a parent class. Every child class reuses it instead of rewriting it.
Polymorphism
The same method name behaves differently depending on the object that calls it.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The loop calls draw() on every shape. The actual code that runs depends on which object s points to — Circle runs Circle's draw, Rectangle runs Rectangle's draw. Same call, different result.
Why it matters: write flexible code that works with many types without knowing the exact type in advance.
Why OOP? — The Real Answer
| Without OOP | With OOP |
|---|---|
| Data is scattered across variables | Data lives inside the object that owns it |
| Any code can change any variable | Only the object's methods can change its data |
| Adding a feature touches many places | Adding a feature usually means one new class |
| Hard to reuse code across programs | Inheritance and composition let you reuse classes |
| Bugs are hard to trace | Each object has clear responsibility — easier to debug |
Common Mistake: Class vs Object Confusion
// WRONG — trying to call a method on a class name
Student.printResult(); // compile error
// CORRECT — create an object first, then call the method
Student s = new Student();
s.printResult();A class is a blueprint. You cannot print a blueprint — you need the actual object built from it.
Practice Task
Task: Create a class called Book with fields title (String) and pages (int). Add a method printInfo() that prints: "Title: [title], Pages: [pages]". In main, create two Book objects with different values and call printInfo() on each.
Hint: The pattern is the same as Student above — define the class, create objects with new, set fields, call the method.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: Run and notice each object prints its own data. Change b1.pages to 100 — b2.pages stays 694. Objects are independent.
Continue the lesson path