Repaso
EspaƱol

What does encapsulation mean in OOP?

Options

  1. Putting all the code of a program together in a single file
  2. Keeping data with its methods, hiding details behind an interface (Correct option)
  3. Copying objects so that the original is never changed
  4. 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.

Key terms

Practice this question in Repaso