OOP Interview Question

What is the difference between abstraction and encapsulation?

Updated 2026-08-16 · 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.

Key takeaways
  • Abstraction hides complexity and shows only essential features, so it is a design decision.
  • Encapsulation hides data by keeping fields private and exposing controlled methods.
  • Abstraction uses interfaces and abstract classes, while encapsulation uses access modifiers like private.

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.

class Payment {                    // abstract base as interface
public:
    virtual void pay(double amount) = 0;  // what it does, not how
    virtual ~Payment() = default;
};
class CardPayment : public Payment {
public:
    void pay(double amount) override {
        // complex card logic hidden here
    }
};
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
    }
}
from abc import ABC, abstractmethod

class Payment(ABC):
    @abstractmethod
    def pay(self, amount):   # what it does, not how
        ...
class CardPayment(Payment):
    def pay(self, 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 {
    int age = 0;                     // hidden data
public:
    void setAge(int a) {
        if (a > 0) age = a;          // controlled change
    }
};
class Student {
    private int age;                 // hidden data
    public void setAge(int a) {
        if (a > 0) age = a;          // controlled change
    }
}
class Student:
    def __init__(self):
        self.__age = 0               # hidden data
    def set_age(self, a):
        if a > 0:
            self.__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.

Frequently asked questions

What is data hiding in OOP?

Data hiding is restricting direct access to an object's fields, usually by making them private, so the internal state can only change through safe methods.

Do abstraction and encapsulation work together?

Yes. A class often abstracts a concept behind a simple interface while encapsulating its data, so the two frequently appear in the same design.

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