Skip to main content
Definition Website Computer and Technology

Encapsulation

Subject: Computer and Technology noun /ɪnˌkæp.səˈleɪ.ʃən/ 1 definition

  1. Definition (noun)

    Entry #131 · Updated

    Encapsulation is a foundational computing principle that bundles data with its governing operations or control metadata inside a self-contained unit and restricts direct external access behind a defined interface. In software design, it pairs internal state with methods inside classes, structures, or modules while hiding implementation details—distinct from abstraction, which defines an external model—so components can uphold invariants and evolve internally without breaking callers. In computer networking, it wraps higher-layer data payloads within lower-layer protocol headers and trailers, enabling independent layers to route, frame, and deliver traffic without interpreting the enclosed contents.

    Common Forms

    • Object-Oriented Encapsulation: Bundles fields and methods within a class or object and regulates visibility using access modifiers (such as private, protected, and public) or accessor properties, forcing external code to interact through validated methods rather than mutating raw state directly.

    • Module and Lexical Encapsulation: Restricts access to internal variables, helper functions, and types at a package, module, or closure boundary—such as unexported identifiers in Go, module visibility in Rust, or private state captured in JavaScript closures—exposing only an explicit public API.

    • Network Protocol Encapsulation: Prepends layer-specific headers and optional trailers around upper-layer protocol data units as traffic descends a network stack—for example, wrapping an HTTP payload inside a TCP segment, an IP packet, and finally an Ethernet frame—before decapsulation strips each layer at the receiver.

    • Tunneling Encapsulation: Encloses an entire network packet or link-layer frame inside the payload of another protocol—as in IPsec tunnel mode, GRE, WireGuard, and VXLAN overlays—to carry encrypted, private, or otherwise non-routable traffic across intermediate networks.

    Example

    In a payment microservice written in Java, a Wallet class declares its balanceCents field private and allows updates only through credit(amount) and debit(amount) methods that reject negative inputs and insufficient funds. Because callers depend solely on those public methods rather than the raw field, engineers can later refactor the backing storage representation or attach an internal audit log without altering any consumer code.