OOP Interview Question

What is the difference between abstraction and encapsulation?

Updated 2026-07-10 · Beginner friendly
Quick answer

Abstraction is about hiding complexity and showing only the essential features, so it happens at the design level. Encapsulation is about hiding data by keeping fields private and exposing controlled methods, so it happens at the implementation level. Abstraction answers what an object does, while encapsulation answers how the data is protected.

The easiest way to remember it

Abstraction hides complexity. Encapsulation hides data. They often work together but they solve different problems. Abstraction is a design decision about what to show, and encapsulation is a coding technique for how to protect the internal state.

Abstraction with a real example

When you send a message on your phone you tap send. You do not think about signals, towers, or network protocols. The app gives you a simple button and hides the hard parts. That is abstraction. In code you achieve it with abstract classes and interfaces.

interface Payment {
    void pay(double amount);   // what it does, not how
}
class CardPayment implements Payment {
    public void pay(double amount) {
        // complex card logic hidden here
    }
}

Encapsulation with a real example

A medicine capsule wraps the ingredients so you cannot touch them directly. In code you make fields private and only allow changes through methods, so no one can put the object into a bad state.

class Student {
    private int age;                 // hidden data
    public void setAge(int a) {
        if (a > 0) age = a;          // controlled change
    }
}

Quick comparison

In the interview

Interviewers ask this to see if you truly understand the concepts or just memorised them. Give the one line difference first, then one real world example for each. Saying abstraction hides complexity and encapsulation hides data is a strong opening.

Want the full OOP guide?

Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the OOP guide All OOP questions