The Four Pillars of OOP — Encapsulation, Inheritance, Polymorphism, Abstraction

2 minute read

OOP is built on four fundamental principles — often called the four pillars. This chapter gives you a high-level overview before we explore each in depth.


The Four Pillars

┌─────────────────────────────────────────┐
│           Object-Oriented Programming    │
├──────────┬──────────┬──────────┬────────┤
│Encapsu-  │Inheri-   │Polymor-  │Abstrac-│
│lation    │tance     │phism     │tion    │
│          │          │          │        │
│Hide data │Reuse     │Same      │Hide    │
│behind    │code from │interface,│complex-│
│methods   │parent    │different │ity,    │
│          │classes   │behavior  │show    │
│          │          │          │essence │
└──────────┴──────────┴──────────┴────────┘

1. Encapsulation — Hide the Details

What: Bundle data and methods together, restrict direct access to data.

Real-world analogy: A TV remote. You press buttons (public interface) without knowing the electronics inside (private implementation).

class BankAccount {
    private double balance;  // hidden

    public void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    public double getBalance() {
        return balance;
    }
}

Key idea: Control how data is accessed and modified.

→ Deep dive: Encapsulation


2. Inheritance — Reuse and Extend

What: A new class inherits fields and methods from an existing class.

Real-world analogy: A child inherits traits from parents but can also develop unique traits.

class Animal {
    void eat() { System.out.println("Eating"); }
}

class Dog extends Animal {
    void bark() { System.out.println("Woof!"); }
}

Dog dog = new Dog();
dog.eat();   // inherited from Animal
dog.bark();  // Dog's own method

Key idea: Build new classes on top of existing ones.

→ Deep dive: Inheritance


3. Polymorphism — Many Forms

What: Same method name behaves differently depending on the object.

Real-world analogy: “Open” means different things: open a door, open a file, open an app.

class Animal {
    void sound() { System.out.println("..."); }
}

class Dog extends Animal {
    void sound() { System.out.println("Woof!"); }
}

class Cat extends Animal {
    void sound() { System.out.println("Meow!"); }
}

Animal a = new Dog();
a.sound();  // "Woof!" — Java calls Dog's version at runtime

Key idea: Write flexible code that works with any subtype.

→ Deep dive: Polymorphism


4. Abstraction — Focus on What, Not How

What: Hide complex implementation, expose only essential features.

Real-world analogy: Driving a car — you use the steering wheel and pedals without knowing how the engine works.

interface PaymentProcessor {
    void processPayment(double amount);
}

class CreditCardProcessor implements PaymentProcessor {
    public void processPayment(double amount) {
        // complex credit card logic hidden
    }
}

class PayPalProcessor implements PaymentProcessor {
    public void processPayment(double amount) {
        // complex PayPal API logic hidden
    }
}

Key idea: Define what something does, let implementations decide how.

→ Deep dive: Abstraction


How They Work Together

In a real application, all four pillars work together:

// Abstraction — interface defines the contract
interface Employee {
    double calculatePay();
    String getName();
}

// Encapsulation — data is private, accessed via methods
class FullTimeEmployee implements Employee {
    private String name;
    private double salary;

    // Inheritance could extend this further
    // Polymorphism — different employee types calculate pay differently
    public double calculatePay() {
        return salary / 12;
    }

    public String getName() { return name; }
}

Summary

Pillar Purpose Mechanism
Encapsulation Protect data private fields + public methods
Inheritance Reuse code extends / implements
Polymorphism Flexible behavior Method overriding + dynamic dispatch
Abstraction Simplify complexity Interfaces + abstract classes

What’s Next?

Let’s start the deep dives — first up: Encapsulation (Principle).

Updated: