Encapsulation
- In Turkish
- Kapsülleme
In short
Encapsulation is the practice of bundling data with the methods that use it and hiding internal details, so outside code works only through a public interface.
What is encapsulation in programming?
Encapsulation means keeping an object's data and the code that works on that data together in one unit, usually a class, and hiding the internal details from the rest of the program. Outside code interacts through a small set of public methods instead of reading and changing the data directly. It is one of the core principles of object-oriented programming, together with inheritance, polymorphism, and abstraction.
Languages enforce encapsulation with access modifiers, keywords that control who can see a member. In Java, C#, and TypeScript, private members can be used only inside their own class while public members form the outside interface; JavaScript marks private fields with a # prefix, and Python relies on the convention of starting a name with an underscore. Because every change goes through methods, the class can validate input and keep its data consistent, for example by refusing to let a bank balance go negative.
A car is a good analogy: you drive with the steering wheel and pedals and never touch the fuel injectors or time the spark plugs yourself. Because those internals are hidden, the manufacturer can redesign the engine without changing how you drive, just as a class can change its internal code without breaking the code that uses it.
Encapsulation is often confused with abstraction. Abstraction is about deciding which essential features an object exposes and ignoring the rest, while encapsulation is the mechanism that bundles the data and hides everything else. Adding a public getter and setter for every private field gives little real encapsulation, since outside code can still change anything; good encapsulation exposes meaningful operations such as deposit() instead.
Key takeaways
- Encapsulation bundles data and the methods that operate on it in one unit.
- Access modifiers such as
privateandpubliccontrol what outside code can see. - All changes go through methods, which can validate input and keep data consistent.
- Internals can change without breaking code that uses the public interface.
- Abstraction decides what to expose; encapsulation hides the rest.
Example
class BankAccount {
#balance = 0; // private: only code inside the class can touch it
deposit(amount) {
if (amount <= 0) throw new Error("Deposit must be positive");
this.#balance += amount;
}
get balance() {
return this.#balance; // read-only from the outside
}
}
const account = new BankAccount();
account.deposit(50);
console.log(account.balance); // 50
// account.#balance = 1000000; // SyntaxError: private fieldReaders ask
What is the difference between encapsulation and abstraction?
Abstraction is about exposing only the essential features of something, such as a send() method on an email client. Encapsulation is how you achieve that in code: bundling data with its methods and hiding the internal details behind access modifiers.
Why is encapsulation important?
It protects data from being changed in invalid ways, keeps related code in one place, and lets you change a class's internals without breaking the code that uses it. That makes large programs easier to maintain and debug.
Does Python support encapsulation?
Python has no enforced private keyword. By convention a leading underscore, as in _balance, marks a member as internal, and a double underscore triggers name mangling, which makes accidental access from outside harder but not impossible.
See also
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- InheritanceProgramming Fundamentals, p. 28Inheritance is an object-oriented programming feature that lets a new class reuse, extend, and override the fields and methods of an existing class.
- PolymorphismProgramming Fundamentals, p. 45Polymorphism is the ability of code to work with values of different types through one shared interface, with each type supplying its own behavior.
- InterfaceProgramming Fundamentals, p. 29An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.
- Separation of ConcernsSoftware Architecture, p. 37Separation of concerns is a design principle that divides a program into distinct parts, each responsible for one clearly defined aspect of its behavior.
- AbstractionProgramming Fundamentals, p. 1Abstraction is hiding the complicated details of how something works behind a simpler interface, so code can use it by knowing only what it does.
Spotted a mistake or something missing on this page?Suggest an edit