The purpose of encapsulation is to hide the internal objects of an object from other objects. The idea is that the outer trace of an object is a certain type, think of it as a contract with other objects. Inside, you may have to jump over some hoops to provide functionality that goes out, but does not apply to other objects. They should not be able to mess with him.
For example, let's say you have a class that provides sales tax calculations. Some kind of utility facility, basically. It has several methods that provide the necessary functionality.
Internally, this class beats the database to get some values (for example, tax for a given jurisdiction) for performing calculations. It may support a database connection and other database related stuff, but other classes don't need to know about it. Other classes are only associated with a functionality contract that goes out.
Suppose that after some time the database should be replaced by an external web service. (The company intends to use the service to calculate sales tax, and not support it domestically.). Since the class is encapsulated, you can change its internal implementation to use the service instead of the database very easily. The class simply must continue to provide the same functions as the external ones.
If other classes deal with inner classes, then reimplementation will risk other components of the system.
David
source share