Java Tutorial
Java Object Oriented Programming
OOP models the program as objects: each one holds data and the methods that work on it. A class is the blueprint; an object is one instance built from that blueprint.
A class is a blueprint
Object-oriented programming means you describe types — a player, an account, a cart line — instead of managing a pile of unrelated variables. The class lists the fields and methods; each object is one concrete instance created with new.
Example
class Account {
String owner;
double balance;
void deposit(double amount) {
balance += amount;
}
}
public class Main {
public static void main(String[] args) {
Account a = new Account();
a.owner = "Ada";
a.balance = 100;
a.deposit(40);
System.out.println(a.owner + " has " + a.balance);
}
}Why bundle data and behavior
Loose int and String variables work until they belong together. A class keeps the balance next to deposit, so you cannot raise one player's score by calling a method meant for another. The next chapters deepen this idea one step at a time.
Example — two objects
class Player {
String name;
int score;
void addPoints(int pts) {
score += pts;
}
}
public class Main {
public static void main(String[] args) {
Player rin = new Player();
rin.name = "Rin";
rin.score = 0;
rin.addPoints(10);
Player kai = new Player();
kai.name = "Kai";
kai.score = 0;
kai.addPoints(7);
System.out.println(rin.name + " " + rin.score);
System.out.println(kai.name + " " + kai.score);
}
}What the OOP section covers
The following lessons build on this sketch: defining classes and objects, instance methods, constructors, access modifiers, encapsulation, inheritance, and polymorphism. Read them in order — each adds one idea without rewriting everything that came before.
You already used classes as data holders earlier. OOP is the same idea plus methods that belong to the object, plus rules for who can see fields and how types relate through inheritance.
Try It Yourself
Exercise: Create a Counter class with an int value field and anincrement() method that adds 1. Create one object, call increment twice, and printvalue.
Show solution
class Counter {
int value;
void increment() {
value++;
}
}
public class Main {
public static void main(String[] args) {
Counter c = new Counter();
c.increment();
c.increment();
System.out.println(c.value); // 2
}
}The method runs on the object, so it updates that object's field — the core OOP habit.
Key Takeaways
- A class is the blueprint; an object is an instance created with
new. - OOP bundles data and the methods that operate on it.
- Each object has its own field values even when they share a class.
- Later chapters add constructors, privacy, inheritance, and polymorphism.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Engineering
Object owns its state
OOP bundles data and behaviour. A Motor knows its rpm and can report whether it is overspeed.
Example
class Motor {
int rpm;
boolean overspeed() { return rpm > 3000; }
}
public class Main {
public static void main(String[] args) {
Motor m = new Motor();
m.rpm = 3200;
System.out.println(m.overspeed());
}
}