Unit 3 · OOP Concepts

3.9Real-World Examples of Encapsulation and Abstraction

On this page
  1. Example 1: ATM
  2. Example 2: Other everyday cases
  3. Practice exercises

Abstraction and encapsulation are easiest to understand in a real system. In each example below, ask two questions: what can the user do? (abstraction) and what is protected, and how? (encapsulation).

Example 1: ATM

ConceptIn the ATM
AbstractionThe user sees only: insert card, enter PIN, choose withdraw, take cash. How the bank verifies the PIN and updates the database is hidden
EncapsulationThe balance is private. It can only be changed through checked operations (withdraw, deposit) that enforce rules
Java
interface ATM {                          // abstraction: what the user can do
    boolean login(String pin);
    void withdraw(double amount);
    double checkBalance();
}

class BankATM implements ATM {
    private double balance = 50000;      // encapsulation: hidden data
    private String pin = "1234";
    private boolean loggedIn = false;

    public boolean login(String entered) {
        loggedIn = pin.equals(entered);
        return loggedIn;
    }
    public void withdraw(double amount) {
        if (loggedIn && amount > 0 && amount <= balance) balance -= amount;
        else System.out.println("Transaction rejected");
    }
    public double checkBalance() { return loggedIn ? balance : -1; }
}
customer«interface»ATMlogin(pin)withdraw(amount)checkBalance()BankATM implements ATMprivate (hidden)balance = 50000pin = "1234"loggedIn = falsemethods check the rulesbefore changing balanceabstraction: what you can doencapsulation: protected data
The ATM example: the interface is the abstraction; the private fields in BankATM are the encapsulation

Reading the ATM code

Java
ATM atm = new BankATM();             // the user only sees the ATM type
atm.login("1234");
atm.withdraw(2000);
System.out.println(atm.checkBalance());   // 48000.0

Example 2: Other everyday cases

SystemAbstraction (what you see)Encapsulation (what is protected)
CarPedals, steering, gear leverEngine state, fuel injection settings
SmartphoneTouch the camera icon to take a photoSensor and image-processing internals
Online shopAdd to cart, payStock counts and payment records, changed only by validated methods
Student record systemView resultsMarks can be changed only by authorised teachers through setters

Example 3: A vending machine

Abstraction: a customer can insert money, choose an item and collect change, which gives the public methods insertMoney(double), selectItem(String code) and getChange(). The customer never sees how the machine counts coins or controls the motors.

Encapsulation: the stock of each item, the prices and the money collected are private attributes. selectItem() only gives out an item if it is in stock and enough money has been inserted, and then it reduces the stock by one. An owner might have a separate restock() method that checks a password first.

This is the same pattern every time: public methods that describe what the user can do, private data that only those methods can change, and rules checked inside the methods.

Practice exercises

  1. Choose a real-world system (vending machine, lift, library). List the abstraction (public operations) and the encapsulated data. Write the Java classes.
  2. Explain to a friend, in one paragraph and without jargon, the difference between abstraction and encapsulation using the ATM example.

End of Unit 3. Test yourself with the Unit 3 questions in the multiple-choice and structured question banks, tick the outcomes in the topic checklist, then go on to the next unit.