Encapsulation
In C, multiple variables can be encapsulated through a structure, representing multiple attributes of a thing. And Java is an object-oriented programming language, it goes further, encapsulates variables and functions in a class, uses the form of objects to simulate multiple aspects of a thing, variables correspond to fields in an object, represent multiple attributes of an object, and functions correspond to the object's methods, simulating the function of the object. So we can represent the state of the object through the value of the Object field, and the object's method represents the function of the object, that is, to pass the data to the object by means of a method and change its state or let it perform some kind of behavior. This is the object's encapsulation of variables and functions.
From another perspective, classes in Java also encapsulate implementations and interfaces. A class encapsulates its internal operating mechanism and provides a range of interfaces to the outside. A consumer of a class can instantiate objects of that class and use them to alter the state of an object or to perform some behavior, complete certain functions, or use it to build more complex objects. The implementation of the class is responsible for implementing the internal operating mechanism of the class and providing the interface to the outside, so these interfaces are the communication protocol between the user and the implementing person. In the development process, the code is not the same, the implementation needs to constantly update the internal operating mechanism to optimize the code, etc., so need a mechanism will be implemented and interface separation, to avoid the implementation of changes in the user's code impact. So, Java uses private, default, protected, public four kinds of access rights mechanism. 1. Code Organization
Before introducing the Java Access Authority mechanism, you need to first understand the Java code organization. Java as an object-oriented language, a class is the basic unit of code, but in addition it has two other levels of basic units, one is the base unit of the library: the package, that is, the library can be composed of multiple packages, the other is a file, that is, a package can be composed of multiple files, Java files are compiled basic units And a file can have more than one class, class is the basic component of Java code units. Java also stipulates that there can be at most one public class (which may not be available) in each file, and that the class should have the same name as the filename. After compilation, each class (including the inner class) generates a bytecode file, the. class file with the same file name as the class name and a slightly different inner class.
Use the package keyword in Java to control the path of the. java file and the. class file, together with the class name to form the fully qualified name of the class and, when loading the class, by searching for the classpath+ (The path of the package control) determines the. class file for the class and loads it into the virtual machine.
So when you use a class, you need the fully qualified name of the class to search for the corresponding. calss file in the classpath and use, in order to reduce the complexity of coding, Java uses the Import keyword to introduce the class, where the simple name of a class is required elsewhere in the file. 2. Access Rights control
Previously, the implementation of the class needs to be separated from the interface, in order to prevent the implementation of changes to the user code, Java in the private, default, protected, public four keywords limit the scope of the class domain and method access. They are in order within the class, within the package, the inheritance structure, the global access scope, the scope is expanded in turn, it is important to note that the protected decorated domain or method, in the package and not within the inheritance structure also has access, that is, protected has a wider range of permissions.
During the implementation of the class, the principle of using access control keywords is to use the domain of the Privatized keyword control class, that is, the attribute, and the method needed to implement the class's operating mechanism, using private to control it within the class, or default (i.e. without keyword). and protected, extended into the package, the inheritance structure within the use of the operating mechanism of the Code control within a certain range of use. For the interface part, the Public keyword is used, which is mainly a method and a negotiated usage agreement between the user and the implementation. Therefore, the implementation of the class and interface are well separated, the implementation can be changed, the interface is relatively fixed, to avoid the impact of changes on users. The change part and invariant partial separation is the core idea of the software design pattern, the Java has supported this idea to some extent through the mechanism of four access rights control keywords.
Finally, the access Rights keyword public and default can be decorated with class, but only one public class in a file can be used by code in the same package as the default class. Private cannot modify a class because it is meaningless outside of a class, and protected is designed for inheritance and has no meaning for classes. Code Reuse 1. Reuse Mode
Reasonable reuse of existing code can greatly improve development efficiency, reuse code is one of the many compelling features of Java. First of all, we need to understand that all things in Java are spread around the class, class is the basic unit of everything, code reuse is no exception. Code reuse can be understood as the reuse of classes, that is, using existing classes to build the new classes you need. Then there are two types of construction, one is the accumulation of wood, which uses an object of an existing class as the domain of a new class, which acts as a property of the new class, expresses its state, and uses the object's capabilities to build the functionality it needs, which is called a combination. Another way of reuse is to transform the way, on the basis of existing classes to extend the class, both retain the original function of the class, but also can extend the new function, this way is called inheritance. There is a tradeoff between the two ways called proxies, and the new classes that agents build are proxy classes, the proxy class contains an object of an existing class (that is, the proxy class) as its property, while the interface exposed by the proxy class is always the interface to the proxy class, while in the implementation of the function the agent is responsible for invoking the corresponding method of the proxy class. That is, the function is implemented by the object of the proxy class, and the user of the proxy class is dependent on the proxy class, regardless of who realizes it. The agent is a design pattern that can be decoupled very well, and here is a simple introduction, a little understanding of the individual. 2. Basic Grammar
Combination and inheritance syntax is relatively simple, here you need to pay attention to the inheritance relationship, the process of object initialization, can be understood to have a hermit in the subclass of the parent class object initialization. In addition, for the problem of methods in the parent class and subclasses, the concept of overwrite and overload is involved for the same method name. For overwrite, the method name and argument list are identical in that the same method is redefined in the parent class in the subclass. Overloading is the same method name in a class, with different argument lists, they are completely different methods, just the same method names because of similar functions. For the same method name in the parent class and subclass, the case of a different argument list is exactly the extension of the interface in the subclass, and it has absolutely nothing to do with the parent class. In order to prevent the attempt to override the parent class method, and the method signature is incorrectly written and extended to the interface, Java uses the @override annotation to indicate that a method is a overwrite of the parent class. If there is a @override annotation due to a written error, there will be a compile-time error reminder. In addition, overloading is independent of the return type, but for overrides, the return type should be consistent, or at least the parent-class method return type or its derived class is returned in the subclass's method, which is called a covariant return type for the return of the derived class. 3. Mode selection
The key question of composition and inheritance is how to select a combination or inheritance. Combinatorial techniques are often used to use the functionality of an existing class in a new class rather than its interface, where an object is embedded in a new class to implement a feature, but the new class's users see only the interfaces defined for the new class, not the interfaces of the embedded object. Inheritance uses an existing class and develops a new version of it, extending its functionality and interfaces. Java is the use of objects to simulate things in the real world, and the choice of composition and inheritance depends to some extent on the relationship between things. In the way of grouping, the relationship between its objects is "has-a", the way of inheritance, and the relationship between its objects is "is-a". Using the combination method, the relationship between the objects is not constrained, the reusable class is more flexible, but the disadvantage is that the new class is a completely new class, in the external and existing classes have no relationship, can not use the upward transition, using the advantages of polymorphism. The inheritance is the extension of the existing class, is a special version of the existing class, can be automatically upward transformation, you can take advantage of polymorphism to reduce the reliance on specific implementation, but the disadvantage is that the existing class and the existence of the relationship between the constraints, use is not so flexible. Final key word
Final represents an immutable type that can modify fields, parameters, methods, and classes. 1.final Domain
Final domain includes two kinds, one is static final domain, is the domain of the class, belong to the compile period constant, need to initialize in the definition, the final static domain that the public modifies, the variable name is usually capitalized, the second is a non-static final domain, that is the immutable attribute of the object, This class is either initialized at definition, or initialized in the constructor, or there will be a compile-time error. 2. Final parameter
The final parameter indicates that the object cannot be modified in this method.
Note that the final modifier variable, whether it is a field or a parameter, for a base type, the value cannot be changed at run time, but for reference types, the object's point cannot be changed, and the state of the object itself can be changed, and no mechanism support is provided in the definition Java for immutable objects. 3. Final method
The final method used to have two scenarios, one is designed to avoid the class in the subclass of this method is covered, the second is to optimize performance, the final method can automatically become an inline function, and not dynamic binding, no polymorphic characteristics, especially high operating efficiency. But as the optimization mechanism of the virtual machine is getting better, the second goal is basically nonexistent. So when considering whether to use the final method, you just need to consider if this is a design requirement.
Also note that the private method is final by default, and subclasses cannot override the private method because subclasses do not have access rights. When you define a method name and a parameter list that is exactly the same as the parent class, you are considered to define a method that has no relation at all. 4. Final class
The final class, very simply, is designed to mean that the class does not want to be extended by the user, that the class cannot be inherited.