Java OOP
OOP stands for Object-Oriented Programming. It is a way of designing programs around "objects" - self-contained bundles of data and the behaviour that acts on that data - instead of writing one long list of instructions. Java was built from the ground up as an object-oriented language, so understanding OOP is the key that unlocks almost everything else you will write in Java.
What is Object-Oriented Programming?
Before OOP, programs were usually written in a procedural style: a stream of functions calling other functions, all sharing loose piles of data. This works for small scripts, but as a program grows it becomes hard to tell which piece of code is allowed to touch which piece of data. OOP solves this by grouping related data and the operations on that data together into a single unit called an object. Each object is responsible for its own state, and the rest of the program talks to it through a clear, public set of methods.
Think of a car. From the driver's seat you use a steering wheel, an accelerator, and a brake. You do not need to know how the fuel injection or the transmission works - those details are hidden inside. In OOP terms, the car is an object: its internal parts are its data, the pedals and wheel are its public methods, and the hidden machinery is its private implementation.
Classes and objects
Two words come up constantly in OOP: class and object. A class is a blueprint - it describes what data an object will hold and what it can do, but it is not a thing you can use directly. An object is a concrete instance built from that blueprint. One class can produce many objects, just as one architectural drawing can be used to build many houses that each have their own address and their own occupants.
A class and the objects made from it
// The blueprint
class Dog {
String name;
void bark() {
System.out.println(name + " says woof!");
}
}
public class Main {
public static void main(String[] args) {
// Two independent objects from the same class
Dog first = new Dog();
first.name = "Rex";
Dog second = new Dog();
second.name = "Bella";
first.bark(); // Rex says woof!
second.bark(); // Bella says woof!
}
}The four pillars of OOP
Object-oriented programming rests on four core ideas. You will meet each of them in detail in later lessons, but it helps to know the big picture now.
- Encapsulation - keeping an object's data private and exposing only safe methods to read or change it, so the object stays in a valid state.
- Inheritance - letting a new class reuse and extend the behaviour of an existing class, so you avoid rewriting shared code.
- Polymorphism - allowing the same method call to behave differently depending on which type of object receives it.
- Abstraction - hiding complex details behind a simple interface, so callers work with concepts rather than mechanics.
Why bother with OOP?
OOP is not just an academic exercise. When code is organised into objects it becomes easier to reuse, easier to test in isolation, and much easier to change without breaking unrelated parts of the program. Large teams can work on different classes at the same time as long as they agree on how those classes talk to each other. This is exactly why nearly every large Java application - from Android apps to banking systems - is built on OOP principles.
Exercise: Java OOP
Which OOP principle is demonstrated by making fields private and exposing them through getters and setters?