Encapsulation

Encapsulation is the practice of bundling data and the methods that act on it inside one unit (like a class) and restricting direct outside access to the data.

General Programming

By keeping internal data private and exposing only safe methods, a class can protect itself from invalid changes. Outside code interacts through a controlled interface rather than reaching in directly.

Examples

Python

class Account:
    def __init__(self):
        self.__balance = 0   # private
    def deposit(self, amount):
        self.__balance += amount
        return self.__balance

print(Account().deposit(50))

Output:

50

See also

Back to the Dictionary