Computer ScienceProgrammingMedium

Encapsulation

Also known as:data hidinginformation hiding

Encapsulation is the OOP principle of bundling an object's data (attributes) and the methods that operate on that data into a single unit (a class), while restricting direct external access to the internal state. By declaring attributes as private and exposing them only through public getter and setter methods, encapsulation enforces data integrity and hides implementation details. This protects objects from unintended interference and simplifies maintenance since internal changes do not affect external code.

Access Modifiers Used in Encapsulation (Java)

Access ModifierWithin ClassSubclass (same pkg)Other Package
privateYesNoNo
default (none)YesYesNo
protectedYesYesNo (subclass only)
publicYesYesYes

Interactive Tools

W3Schools – Java Encapsulation

Open Tool

Codecademy – Encapsulation in Python

Open Tool

Khan Academy – OOP Principles

Open Tool
Diagram showing a class capsule with private data protected by public methods

Wikimedia Commons, CC BY-SA

Related Terms

From Latin "capsula" meaning "small container" or "case", a diminutive of "capsa" (box). In programming, the concept was central to Simula 67's modules and became explicit in Smalltalk's design philosophy. The term emphasises wrapping data as if in a protective capsule.

encapsulationoopaccess-controldata-hidinggetters-setters