Object-Oriented Programming (OOP) is the backbone of Java. Once you understand it, everything else in Java falls into place naturally. This guide walks through all four pillars of OOP — using simple, real-world analogies that make sense to a school student.
In this article
What is OOP?
Object-Oriented Programming is a programming paradigm built around objects. Instead of writing a long list of instructions (procedural programming), OOP organises code into self-contained units — objects — that have both data (properties) and behaviour (methods).
Car class is the blueprint; each actual car you create is an object.
Classes and Objects
A class is a template or blueprint. An object is an instance of that class — a real, in-memory entity created from the blueprint.
public class Car {
String color;
int speed;
void accelerate() {
speed += 10;
System.out.println("Speed: " + speed);
}
}
// Creating objects
Car myCar = new Car();
Car herCar = new Car();
Here, myCar and herCar are two distinct objects, each with their own color and speed values, even though they come from the same Car class.
Encapsulation
Encapsulation means bundling data and the methods that operate on it into a single unit (the class), and hiding the internal details from the outside world using access modifiers like private.
public class BankAccount {
private double balance; // hidden from outside
public void deposit(double amount) {
if(amount > 0) balance += amount;
}
public double getBalance() {
return balance;
}
}
Encapsulation is the principle of "need to know" — the outside world only sees what it needs to. Implementation details stay private.
Inheritance
Inheritance allows a new class (child) to acquire properties and methods of an existing class (parent). This promotes code reuse and creates an is-a relationship.
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Woof!"); }
}
Dog d = new Dog();
d.eat(); // inherited from Animal
d.bark(); // Dog's own method
Polymorphism
Polymorphism means "many forms". In Java, the same method name can behave differently depending on the object or context. There are two types:
- Compile-time polymorphism — achieved through method overloading (same method name, different parameters).
- Runtime polymorphism — achieved through method overriding (child class redefines a parent class method).
🎯 Exam Tips
- Always define access modifiers — examiners look for
privateon data members. - Remember: a constructor has the same name as the class and no return type.
- Draw a UML-style box diagram if you get a class-design question — it earns presentation marks.
- Method overloading ≠ method overriding. Know the difference clearly.