---
title: Encapsulation (computer programming)
slug: encapsulation-computer-programming
qid: 
snapshot: enwiki-001338241453
content_hash: 
license: CC BY-SA 4.0
source: https://en.wikipedia.org/w/index.php?oldid=1338241453
tags: [22961575, 23118434, 23603337, 24745122, 25927622, 26157654, 31128806]
---

# Encapsulation (computer programming)

> [!note] Provenance
> Snapshot `enwiki-001338241453` · content root `…`
> Derived from <https://en.wikipedia.org/w/index.php?oldid=1338241453> · Licensed CC BY-SA 4.0

In software systems, encapsulation refers to the bundling of data with the mechanisms or methods that operate on the data. It may also refer to the limiting of direct access to some of that data, such as an object's components. Essentially, encapsulation prevents external code from being concerned with the internal workings of an object. Encapsulation allows developers to present a consistent interface that is independent of its internal implementation. As one example, encapsulation can be used to hide the values or state of a structured data object inside a class. This prevents clients from directly accessing this information in a way that could expose hidden implementation details or violate state invariance maintained by the methods. Encapsulation also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages decoupling. All object-oriented programming (OOP) systems support encapsulation, but encapsulation is not unique to OOP. Implementations of abstract data types, modules, and libraries also offer encapsulation. The similarity has been explained by programming language theorists in terms of existential types.

In software systems, encapsulation refers to the bundling of data with the mechanisms or methods that operate on the data. It may also refer to the limiting of direct access to some of that data, such as an object's components. Essentially, encapsulation prevents external code from being concerned with the internal workings of an object. Encapsulation allows developers to present a consistent interface that is independent of its internal implementation. As one example, encapsulation can be used to hide the values or state of a structured data object inside a class. This prevents clients from directly accessing this information in a way that could expose hidden implementation details or violate state invariance maintained by the methods. Encapsulation also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages decoupling. All object-oriented programming (OOP) systems support encapsulation, but encapsulation is not unique to OOP. Implementations of abstract data types, modules, and libraries also offer encapsulation. The similarity has been explained by programming language theorists in terms of existential types. class Program { public class Account { private decimal _accountBalance = 500.00m; public decimal CheckBalance() { return _accountBalance; } } static void Main() { Account myAccount = new(); decimal myBalance = myAccount.CheckBalance(); /* This Main method can check the balance via the public * "CheckBalance" method provided by the "Account" class * but it cannot manipulate the value of "accountBalance" */ } } Below is an example in Java: public class Employee { private BigDecimal salary = new BigDecimal(50000.00); public BigDecimal getSalary() { return this.salary; } public static void main() { Employee e = new Employee(); BigDecimal sal = e.getSalary(); } } Encapsulation is also possible in non-object-oriented languages. In C, for example, a structure can be declared in the public API via the header file for a set of functions that operate on an item of data containing data members that are not accessible to clients of the API with the extern keyword. // Header file "api.h" pragma once typedef struct Entity Entity; // Opaque structure with hidden members // API functions that operate on 'Entity' objects Entity* openEntity(int id); int processEntity(Entity* e); void closeEntity(Entity* e); Clients call the API functions to allocate, operate on, and deallocate objects of an opaque data type. The contents of this type are known and accessible only to the implementation of the API functions; clients cannot directly access its contents. The source code for these functions defines the actual contents of the structure: // Implementation file "api.c" include "api.h" typedef struct Entity { int ent_id; // ID number char ent_name[20]; // Name ... and other members ... } Entity; // API function implementations Entity* openEntity(int id) { // ... } int processEntity(Entity* e) { // ... } void closeEntity(Entity* e) { // ... } class Car: def __init__(self) -> None: self._maxspeed = 200 def drive(self) -> None: print(f"Maximum speed is {self._maxspeed}.") redcar: Car = Car() redcar.drive() # This will print 'Maximum speed is 200.' redcar._maxspeed = 10 redcar.drive() # This will print 'Maximum speed is 10.'
