Encapsulation is hierarchical
When people are talking about object oriented designs, one common problem they are facing is deciding if a member of a class should be open to everyone outside or should just kept known by the class itself. The answer for this fundamental question is never to be simple and never to be unique. The best answer for this problem probably should be “don’t make primitive things globally public”.
Public members of a class should only be those ones which are necessary for other classes to make use of the class. Other details which are not directly relevant or necessary for other class to use should be kept private. And this rule should be further extended to other program units, i.e. might not always be talking about class, the boundary of the encapsulation could be of method, class, package/modules.
Several benefits have already be drawn from this:
- It keeps a cleaner “interface” for others to use, all the detailed information are kept private so they will not interfere the clean interface the class has.
- Avoid the danger of people making use of the parts which you are no planning to support (in such a way a user of your class might assume)
- As results, a clean interface can be provided and still give you the class creator the freedom of changing detailed bit inside of the class.
External Links
More interesting comments can be found at https://c2.com/cgi/wiki?EncapsulationIsHierarchical