What does encapsulation mean in OOP?
Options
- Putting all the code of a program together in a single file
- Keeping data with its methods, hiding details behind an interface (Correct option)
- Copying objects so that the original is never changed
- Using as many small classes as possible in the design
Answer
Keeping an object's data and the methods that use it together, and hiding internal details behind a public interface
Why
Encapsulation bundles data with the methods that work on it, and hides the internal details.
Example: a BankAccount exposes deposit() and withdraw(), but nobody outside changes balance directly, so the class can enforce rules like 'no negative balance'.
In Python a leading underscore (_balance) signals 'internal, do not touch'.
Remember
Encapsulation = hide the internals, expose a safe interface.