There are two reasons to control access permissions for members:
The first is to prevent users from touching the parts they should not touch.These parts are necessary for internal operations of the class, but they are not part of the interface required by the client programmer. Therefore, specifying methods and domains as private isService. In this way, they can clearly see what is important to them and what they can ignore. This simplifies their understanding of classes.
The second and most important reason is that,To enable the class library designer to change the internal working mode of the classWithout worrying about the impact on client programmers. For example, you may first create a class in some way, and then find that if you change the program structure, it can greatly improve the running speed.
If interfaces and implementations can be clearly isolated and protected, this can be achieved without forcing the client programmer to rewrite the code.Access control ensures that no client programmer is dependent on any part of the underlying implementation of a class..
When we have the ability to change the underlying implementation details, we can not only improve the design at will, but may also make mistakes at will, and at the same time, there is a possibility of making mistakes. No matter how careful you plan and design it, you may make mistakes. When you learn that mistakes are relatively safe, you can experiment more securely and learn how to complete projects faster.
The public interface of the class is actually visible to the user. Therefore, this part determines whether the class is correct during analysis and design.The most important part. However, you still have room for changes. If you cannot create the correct interface at the beginning, you can add more methods in the future without deleting anything that the client programmer has used in their program.
Final data
For basic types, final makes the data constant; for object reference, final makesConstant reference. OnceReferenceIf it is initialized to an object, it cannot be changed to another object. However,The object itself makes it changeableJava does not provide any way to keep objects unchanged. This restriction also applies to arrays and is also an object.
Final Method
"Override" only appears when a method is part of the interface of the base class. That is, you must be able to transform an object to its basic type and call the same method. If a method isPrivate, It isNot part of the base class Interface. It is only some code hidden in the class, but it has the same name. However, if the export class generates a public, protected, or package access permission method in the same way, this method does not generate a condition where the base class only has the same name. At this time, you did not overwrite this method, but only generatedNew method.Because
The private method cannot be touched and can be effectively hidden. Therefore, apart from seeing it as because of the organizational structure of the class to which it belongs, no other things need to consider it.
Inheritance and combination
Both inheritance and combination can generate new types from existing types. The Combination usually uses the existing type as the new type.Underlying implementationWhile inheritance reuse isInterface. Because the export class has a base class interface, it canUpward TransformationTo the base class.PolymorphismIt is crucial.
Although object-oriented programming emphasizes inheritance, a combination should be preferred when starting a design, and inheritance should be used only when necessary. Because the combination is more flexible. In addition, the inheritance technology is used for member metadata.(I don't understand it here. I think it is a combination of technologies. Solution !)Adding skills,You can change the type and behavior of member objects at runtime.. Therefore, you can change the behavior of a combined object at runtime.
When designing a system, the goal should be to find or create some classes, each of which has a specific purpose and is neither too large (it contains too many functions and is difficult to reuse ), it will not be too small (it cannot be used without adding other features ).
"Override" Private method in polymorphism (see final method)
public class PrivateOverride{ private void f(){ System.out.println("private f()"); } public static void main(String a[]){ PrivateOverride po = new Derived(); po.f(); }}class Derived extends PrivateOverride{ public void f(){ System.out.println("public f()"); }}
The expected output is"Public F ()"(The output is" Private F () "), but the private method is automatically consideredFinalMethod, and the export class is blocked. Therefore, the F () method in the derived class is a brand new method in this case. Since the F () method in the base class is invisible to the subclass derivedNot overloaded.
The conclusion is:Only non-private methods can be reloaded.But we also need to pay close attention to the phenomenon of overloading the private method. Although the compilation will not go wrong, it will not be executed as we expected. Clearly, in the export class, we 'd better use a different name for the private method in the base class. ,
The above content is compiled from Java programming ideas.