Skip to main content

Data abstraction versus encapsulation

Short answer

Data abstraction hides implementation details, showing only essential features. Encapsulation binds data and functions into a class, hiding data to prevent accidental change. Abstraction is design-level, encapsulation is implementation-level; encapsulation enables abstraction.

What is data abstraction? Compare it with encapsulation in C++.

Data abstraction

Data abstraction means showing only the essential features of an object to the outside world and hiding the background details of how they work. The user knows what an object does, not how it does it. In C++ it is achieved with classes (public interface and private data) and abstract classes. For example, with cout << x; we use the output facility without knowing how characters reach the screen; a Stack class offers push() and pop() while the array and index stay hidden.

Comparison with encapsulation

Encapsulation is wrapping data and the functions that work on it into one unit (a class) and restricting direct access to the data.

PointData abstractionEncapsulation
MeaningHiding implementation details, showing only essentialsBinding data and functions together and hiding the data
LevelDesign level (what to show)Implementation level (how to hide)
PurposeReduce complexityProtect data and keep integrity
Achieved byAbstract classes, interfaces, public member functionsClasses with private / protected members
ResultUser sees a simple interfaceData cannot be changed accidentally

Encapsulation is the mechanism that makes abstraction possible in C++.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 1 questions →