DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Abstraction and Encapsulation

Abstraction and Encapsulation

The fine line difference between abstraction and encapsulation can be summarized in a single key point:

Abstraction is about hiding unnecessary details to show only the essential features (what an object does). It focuses on design and simplification of complex systems.

Encapsulation is about hiding the internal implementation and protecting data by restricting access to certain components (how an object’s internal data and methods work). It focuses on security and controlled interaction with data.

In short:

Abstraction: Focuses on what the object can do.

Encapsulation: Focuses on how the object's internal state is protected.


Here’s a real-life example to illustrate the fine line difference between abstraction and encapsulation:

ATM Machine (Automated Teller Machine)

1. Abstraction (What it does):

When you use an ATM, you only interact with essential functions like:

Withdraw money

Check balance

Deposit money

You don’t need to know how these operations are implemented internally, such as the complex processes happening inside the bank’s software, the communication with the bank’s servers, or how the transaction is processed. The ATM abstracts all these complexities and provides you with a simple interface (what you can do with it).

2. Encapsulation (How it protects):

Inside the ATM, your personal information (PIN, account balance, etc.) is stored and protected. You can only access this information through controlled methods like:

Entering the correct PIN.

The machine encapsulates (hides) your data, ensuring that no one else can directly access or modify your balance without using the proper channels (like authentication). This encapsulation ensures your data is secure and can only be accessed in a controlled way.

Top comments (0)