Encapsulation is one of the defining pillars of object-oriented programming. It refers to bundling an object's data together with the methods that operate on that data, and — crucially — controlling access to that data so that the object's internal details are hidden from the outside world. This second idea is called data hiding or information hiding, and it is what makes an object a trustworthy, self-managing unit rather than a loose bag of variables anyone can meddle with.
The main tool for controlling access is the access modifier, a keyword placed in front of an attribute or method that specifies who is allowed to use it. The three most common modifiers are:
private — the member is accessible only from within its own class. This is the default choice for attributes.p