The most important point for any relationship is to set the boundaries or rules that all parties must observe. Creating a library is equivalent to establishing a relationship with that library's users, the "client programmer," who belong to another programmer, may build an application with our library, or build a larger library with our library.
Without a rule, a client programmer can manipulate all members of a class at will, regardless of whether we would have preferred some of those members to be directly manipulated. Everything is exposed in front of others.
This chapter describes how to build a class to produce an ideal library. First, we describe how to encapsulate a set of classes into a library. Second, we describe how the class controls access to its members.
In general, a C program project starts to break somewhere between 50K and 100K lines of code. This is because C has only one "namespace", so the names start to contradict each other, resulting in additional administrative overhead. In Java, the package keyword, the package naming scheme, and the Import keyword give us full control of the name, so the problem of naming conflicts can easily be avoided.
There are two reasons that require us to control access to members. The first is to prevent users from touching tools that they should not touch. For internal mechanisms of data types, those tools are required. But they are not part of the user interface, and users do not have to use it to solve their own specific problems. Therefore, the method and the field into a "private" (private), can greatly facilitate the user. Because they can easily see what is most important to themselves and what they need to ignore. This simplifies the user's understanding of a class.
The second and most important reason for access control is to allow the library designer to change the internal working mechanism of the class without worrying about what impact it will have on the client programmer. At the very beginning, you can build a class in one way, and later discover that you need to rebuild your code to get faster. such as interface and implementation details have already been clearly separated and protected, you can easily achieve their own purposes, do not require users to rewrite their code.
Using the access indicator in Java, you can effectively control the creator of the class. Users of that class can know exactly what they can use and what they can ignore. But more importantly, it ensures that no user can rely on any part of a class's underlying implementation mechanism. As the creator of a class, we are free to modify the implementation details of the foundation, a change that does not have any impact on the client programmer because they cannot access that part of the class.
The ability to change the basis of the implementation of the details, in addition to improving their own settings in the future, but also has the "make mistakes," the freedom. No matter how carefully you plan and design, you may still have some mistakes. Knowing that he can make such a mistake quite safely, it is safe to boldly carry out more and more free trials. This is helpful to improve your programming level so that the whole project can be done faster and better.
A common interface for a class is visible to all users, so it is the most important part of the analysis and design to ensure its accuracy. But there is no need to be too nervous, and a little error is still allowed. If there is a slight problem with the initial design interface, consider adding more methods, as long as you are not removing what the client programmer has already used in their code.