The Four Pillars
Encapsulation, Inheritance, Polymorphism, and Abstraction - each with a real-life example.
On this page
Object-oriented programming rests on four pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction. Master these and you understand the soul of Java. We'll take them one at a time, each with a real-life example.
Pillar 1 - Encapsulation
Encapsulation means bundling data with the methods that operate on it, and hiding the internal details behind a controlled interface.
A capsule wraps the medicine inside a protective shell. You interact with it simply - swallow it - without touching the powder directly. An object works the same way: it hides its internal data and exposes a few safe methods. You use the object without meddling with its insides.
Make fields private; expose public methods (getters and setters) that
guard access:
public class Thermostat {
private int temperature = 20; // hidden
public int getTemperature() { // read
return temperature;
}
public void setTemperature(int value) { // write, but guarded
if (value >= 5 && value <= 35) {
temperature = value;
}
}
}Nobody can set the thermostat to 1000°. The object protects its own state.
Pillar 2 - Inheritance
Inheritance lets a class reuse and extend another class. The child (subclass) inherits the parent's (superclass) fields and methods, then adds or changes behavior.
A child inherits traits from a parent, then develops their own. In code, a
Car and a Truck are both Vehicles - they share wheels and the ability to
drive, but each adds its own specialties.
public class Vehicle {
String brand;
void start() {
System.out.println(brand + " is starting.");
}
}
public class Car extends Vehicle { // Car IS-A Vehicle
int doors;
void honk() {
System.out.println("Beep beep!");
}
}Car car = new Car();
car.brand = "Toyota";
car.start(); // inherited from Vehicle → "Toyota is starting."
car.honk(); // Car's own method → "Beep beep!"Inheritance means 'is-a'
Only use inheritance for a true "is-a" relationship: a Car is a Vehicle. If you catch yourself saying "has-a" (a Car has an Engine), prefer composition
- building objects from other objects - which you'll learn in the design principles stage.
Pillar 3 - Polymorphism
Polymorphism ("many forms") lets the same method call behave differently depending on the actual object. A subclass can override a parent method to provide its own version.
Imagine a remote with one Speak button. Point it at a dog and it barks; at a cat and it meows; at a cow and it moos. Same button, different behavior based on the animal. That's polymorphism.
public class Animal {
String sound() { return "..."; }
}
public class Dog extends Animal {
@Override
String sound() { return "Woof"; }
}
public class Cat extends Animal {
@Override
String sound() { return "Meow"; }
}Animal[] animals = { new Dog(), new Cat(), new Animal() };
for (Animal a : animals) {
System.out.println(a.sound()); // Woof / Meow / ...
}Even though the variable's type is Animal, Java calls the actual object's
version of sound(). The @Override annotation tells Java (and readers) you're
intentionally replacing a parent method.
Pillar 4 - Abstraction
Abstraction means exposing what something does while hiding how it does it. You focus on the essential interface and ignore the messy details.
To drive, you use the steering wheel, pedals, and gear stick. You don't need to know how fuel injection or the transmission works. The car abstracts away its complexity behind a simple interface. Good code does the same.
In Java, you express abstraction with abstract classes and interfaces, which define what methods exist without (necessarily) saying how:
abstract class Shape {
abstract double area(); // WHAT: every shape has an area (no HOW here)
}
class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
@Override
double area() { return Math.PI * radius * radius; } // the HOW
}Anyone using a Shape can call area() without caring whether it's a circle,
square, or triangle. We'll go deeper on this in the next lesson.
The four pillars at a glance
| Pillar | Core idea | Real-life analogy |
|---|---|---|
| Encapsulation | Hide data behind safe methods | A medicine capsule |
| Inheritance | Reuse & extend a parent class | Family traits |
| Polymorphism | One call, many behaviors | A universal "speak" button |
| Abstraction | Show what, hide how | Driving a car |
Quick check
A subclass provides its own version of a method already defined in its parent. This is called…
Key takeaways
- Encapsulation hides data behind controlled methods, keeping objects valid.
- Inheritance lets a subclass reuse and extend a superclass ('is-a' relationships).
- Polymorphism lets one method call behave differently per object, via method overriding (@Override).
- Abstraction exposes what an object does and hides how, using abstract classes and interfaces.
- Together, the four pillars make code reusable, flexible, and easier to maintain.
Next, we'll zoom into the two tools of abstraction you just glimpsed: interfaces and abstract classes - and when to use each.