Java Access Rights control

Source: Internet
Author: User
Tags modifiers

access Rights Control  Java provides access modifiers for class library developers to indicate to the client programmer what is available and what is not. The level of access control, from the maximum permissions to the minimum permissions are: Public, protected, package access (no keywords), private. Package: library UnitThere is a set of classes within the package that are organized together under a single namespace. If you are importing a class from a standard library, you can use the Import keyword. The reason we want to import is to provide a mechanism for managing namespaces. The names of all class members are separated from each other.        So different classes with the same method do not have errors when the program is running.        Due to the potential conflict between names, it is important to have full control of the Java Squadron namespace and to create a unique combination of identifiers for each class. When writing a Java source code program, this file is often referred to as Compilation Unit(Sometimes it is also a translation unit). Each compilation unit must have a suffix name. java。 Within the compilation unit, you can have a Public class, the name of the class must be the same as the name of the file (including case, but not the suffix name of the file. Java). Each compilation unit can have only one public class, or the compiler will not accept it. If there are additional classes in the compilation unit, then the world outside the package cannot see these classes because they are not public types. Code Organization: When compiling a. java file, each class in the. java file will have an output file with the same name as each class in the. java file, with just one more suffix. class. After compiling a small number of. java files, you get a large number of. class files . Java can run programsis a set of. class files that can be packaged and compressed into a Java document file (jar, which uses Java's Jar document generator). The Java interpreter is responsible for locating, loading, and interpreting these files. A class library is actually a set of class files. Each of these files has a public class, and any number of non-public classes, so each file has a component. If you want these constructs (each with their own. Java and. class files) to belong to the same group, you can use the keyword package. If you use the package, it must be the first line of code in addition to the comment.          Create a unique package name:Using the operating system's Hierarchy ofFile structure to solve the problem of confusion in Java.  The first part of the package name is the anti-order Internet domain name of the creator of the class. The second part is to break down the package name into a directory on your machine. So when the Java program runs and needs to load the. class file, it can determine where the. class file is located in the directory. The Java interpreter runs the process:   Conflict:This potential conflict may occur if you import all two class libraries of the same name with the wildcard "*", and there may be one class in the two class library that appears. The class library of the instantiated class must be explicitly indicated. Or, when importing a class library, use only the classes that you need, not wildcard characters. java access modifier words        Public, protected, and private these Java access modifiers, when used, are placed before the definition of each member in the class, whether it is a domain or a method. each permission modifier controls only the access to the specific definition that it modifies. If no access modifier is provided, it means "package access."   Package access rights:The default access permission does not have any keywords. This means that all other classes in the current package have access to that member, but for all classes outside of the package, this member is private. With the package access permission, all classes in the same compilation unit are automatically accessible. Package access allows all related classes within a package to be combined so that they can interact easily with each other.   Public : Interface access rights         Using the keyword public means that the members immediately following the public claim that they are available to everyone. Default Package: The two classes that are in the same directory and do not set any package names to themselves are accessible to each other, because Java automatically sees such files as the default packages that are subordinate to the directory. They provide package access for all other files in the directory.   Private: You cannot access         In addition to the class that contains the member, no other class can access the member. Private allows you to change the member at will, regardless of the other classes within the package that are affected by this.        The use of private is very important, especially in the multi-threaded environment. Any method that is sure to be just an "assistant" method of the class can designate it as private to ensure that it is not misused elsewhere in the package, and therefore prevents you from changing or deleting the method.   protected: Inheriting access rights         The keyword protected deals with InheritanceConcept, inheritance allows you to add a new member to an existing class without touching the existing class by using an existing class (the base class). Love can change the behavior of existing members of the class.        Inherits an existing class with extends. You want a specific member to assign access to it to a derived class rather than to all classes. You can use protected to do the work. Protected also provides package access, meaning that other classes within the same package can access the protected. Interfaces and implementations         Control of access rights is often referred to as concrete implementation of the hidden.         Wrapping data and methods into classes, and the concealment of concrete implementations, are often collectively referred to as encapsulation. The result is a data type with characteristics and behavior at the same time. For two very important reasons, access control delimits the bounds of the permission within the data type. 1. Set the bounds that the client programmer can use and not be able to use. Build your own internal mechanism in the structure without worrying about access errors.        2. Detach the interface from the specific implementation. The task of presenting an interface to a class is determined by the Class BrowserTo complete,   the access rights of the class in Java, access permission modifiers can also be used to determine which classes in the library are available to the consumer of the library. The class cannot be private (this will make it inaccessible to any class other than the Class), nor can it be protected. Therefore, the access to the class can only be the package access permissions or public. If you do not want anyone to have access to the class, you can designate all the constructors as private, preventing anyone from creating objects of that class, but with one exception, you can create inside the static members of the class.    

Java Access Rights control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.