# Why Object-Oriented Programming — Object-Oriented Programming (OOP)

Source: https://www.skillbyai.com/en/oop/f-why

> Compare procedural and object-oriented styles and name the four pillars.

## Organising code around things, not steps

In **procedural programming**, a program is a sequence of procedures (functions) operating on data structures that are passed around; any function can read or change any data. That works well for small programs, but as systems grow, it becomes hard to know which functions depend on which data, and a change to a data structure ripples everywhere. **Object-oriented programming (OOP)** organises code around **objects** that bundle **data** (state) with the **operations** (behaviour) allowed on that data. An `Account` object holds its balance and offers `deposit` and `withdraw`; other code cannot set the balance directly. OOP is traditionally described by four pillars: **encapsulation** (hide internal state behind a public interface), **abstraction** (expose what an object does, not how), **inheritance** (reuse and specialise behaviour from a parent type) and **polymorphism** (one interface, many implementations). Java, C++, C#, Python, Kotlin and Swift are all object-oriented, though each mixes in other styles too.

## Data and behaviour together

Procedural code keeps data and functions apart; objects bundle them behind an interface.

![Left: separate data boxes with many arrows from loose function circles. Right: capsules each containing a data block wrapped by a ring of method dots.](assets/figures/oop/section-1-map.svg) — Figure 1.1 — Procedural data and functions versus objects.

## The same idea in two styles

In the object version, only Account's own methods can change its balance.

```java
// procedural style: data and functions are separate
class AccountData { double balance; }

class Banking {
    static void withdraw(AccountData a, double amount) {
        a.balance -= amount;                 // any code could also do a.balance = -1000
    }
}

// object-oriented style: data and behaviour together, state protected
class Account {
    private double balance;

    public void deposit(double amount) {
        if (amount <= 0) throw new IllegalArgumentException("amount must be positive");
        balance += amount;
    }

    public void withdraw(double amount) {
        if (amount <= 0 || amount > balance) throw new IllegalArgumentException("invalid amount");
        balance -= amount;
    }

    public double getBalance() { return balance; }
}
```

## OOP is a tool, not a religion

Modern code mixes OOP with functional techniques: immutable values, pure functions and lambdas. Use objects where state and behaviour belong together, and plain functions where they do not.

**Quiz:** Which list names the four commonly cited pillars of OOP?

- [x] Encapsulation, abstraction, inheritance, polymorphism
- [ ] Variables, loops, functions, arrays
- [ ] Compilation, linking, loading, execution
- [ ] Classes, files, packages, modules

*Answer:* Encapsulation, abstraction, inheritance, polymorphism. These four principles are the classic foundation of object-oriented design.
