encapsulation - when the internal state of objects can be changed only by the object itself (the ability of the object to maintain its internal state).
What is the object:
It is an entity that simultaneously contains behavior and data. Physical this entity is located in the address space of the computer (also in one place in memory).
An object has attributes such as attributes, states, behaviors.
An object can be a class, a group of classes, a subsystem.
What is the state of the object:
For example, if a person dyed his hair, then his state changed. hair color property has changed values.
Example: There is some abstract brain that can take information in a linear fashion.
1) The stage of data hiding
class Brain{ String capsule; } We have already hidden the data by creating the Brain class. Now it can be accessed only by creating an instance of this class. There are no other ways to reach the capsule. Thus, the internal state of Brain can only change an object of this class.
We hide data by creating a Brain class from the outside world. The races attributes are hidden from the outside world; only the object itself can change its internal state.
brain1 = new Brain(); brain2 = new Brain(); brain1.capsule = "Чайник"; brain2.capsule = "Магазин"; 2) Stage hidden implementation details
class Brain{ private String capsule; //Интерфейс методы для взаимодействия с объектом.(Слово интерфейс в широком понимании) public String getCapsule(){ return capsule; } public void setCapsule(String data){ if(!isBusy){ //проверям является ли этот информация корректным //если да то проверям может ли мозг принят его if(isCorrect(data) && isAccept(data)) capsule = data; }else{ waitPleas(); } } //Реализация private boolean isCorrect(String data){ } //Реализация private boolean isAccept(String data){ } private boolaen isBusy(){ } } The Brain class has limited access to the data and has hidden implementation details. The client was given only interface methods.