Java path (5) access control and java permission Control

Source: Internet
Author: User

Java path (5) access control and java permission Control

In Java, all things have some form of access control.

The maximum and minimum access control levels are public, protected, package access permissions (without keywords), and private.

When using the public, protected, and private Java access permission modifiers, they are placed before the definition of each member (domain or method) in the class.

I. Access Permissions of Class Members

The only way to gain access to a member is:

1) make the member public. Anyone who is there can access this member;

2). Assign the package access permission to members by leaving the modifier with no access permission and placing other classes in the same package. Other classes in the package can access the member;

3) The inherited class can access both public and protected members.

4) provides accessors and mutators to read and change values.

1. Package access permission

The default access permission does not have any keywords, but the pass means the package access permission. This means that all other classes in the current report have access permissions to that member, but for all classes outside the package, this member is indeed private.

The package access permission combines all related classes in the package so that they can interact with each other easily.

Note: If the two classes are in the same directory and no package name is set for themselves, Java will automatically regard such a file as the default package to which the directory belongs, therefore, these files have Packet Access Permissions between them.

The following example illustrates the problem:

// Classes Cake and Pie are in the same directory and are not explicitly displayed in any package. class Pie {void f () {System. out. println ("Pie. f () ") ;}} class Cake {public static void main (String [] args) {Pie x = new Pie (); x. f () ;}// the output is Pie. f ()

 

2. public: interface access permission

Using the public keyword means that the subsequent member declaration is available to everyone, especially the client programmers who use the class library.

3. private: you cannot access

The keyword private indicates that no other class can access this member. Other classes in the same package cannot access the private Members of this class, so this is equivalent to isolating yourself.

This role of private keywords has many purposes, such as controlling how to create objects and preventing others from directly accessing a specific Constructor (or all constructor ). See the following example:

class Sundae{    private Sundae(){}    static Sundae makeASundae(){        return new Sundae();    }}public class IceCream {    public static void main(String[] args){        Sundae x = Sundae.makeASundae();    }}

In this example, we can call the makeASundae () method to create a Sundae object, but not the constructor.

This applies to private fields in the class.

Note that other objects cannot own the public reference of an object because the reference of an object in the class is private.

4. protected: inherits the access permission

If a new package is created and the class is inherited from the other package, the only accessible member is the public Member of the source package.

Sometimes, the creator of the base class wants to grant the access permission of a specific member to the derived class rather than all classes. This requires the use of the keyword protected.

Note that protected also provides the package access permission, that is, other classes in the same package can also access the protected element of this class.

Ii. interfaces and Implementations

Access control is usually called the hiding of specific implementations.

Package data and methods into the class, and hide specific implementations, which are often called encapsulation.

For two important reasons, access control divides the boundaries of permissions within the data type:

1. Set the boundary between the use and unavailability of client programmers. You can build your own internal mechanism in the structure, so you don't have to worry about the client programmer's occasional use of the internal mechanism as part of the interface they use.

2. Separate interfaces from specific implementations.

Iii. Class Access Permissions

In Java, the access permission modifier can also be used to determine which classes in the library are available to users of the library.

The modifier must be placed before the keyword class. For example:

Public class Widget {...} Or improt access. Widget;

You must know that the class cannot be private (if the class is private, no other class except the class can access it ), it cannot be protected (in fact, an internal class can be private or protected, but this is a special case, as described in later articles). It can only be package access permission or public.

If you do not want others to access this class, you can specify all constructors of this class as private to prevent anyone from creating objects of this class. But this is also an exception. This method cannot prevent you from creating the class within the static member of the class. Let's take a look at the following example:

Class Soup1 {private Soup1 () {} public static Soup1 makeSoup () {// use the static method to create the object return new Soup1 () ;}} class Soup2 {private Soup2 () {} private static Soup2 ps1 = new Soup2 (); // create the object public static Soup2 access () {return ps1;} public void f () in singleton Mode () {}} public class Lunch {void testPrivate () {// Soup1 soup = new Soup1; cannot be executed} void testSingleton () {Soup2.access (). f ();}}

We can see that Soup1 and Soup2 constructors are private, and no one can directly use the constructors to create objects of this class. However, we can also use these two classes: Create a static method in Soup1 and use the constructor in this method to create a Soup1 object and return its reference; the creation of Soup2 uses the singleton mode in the design mode. Only one object of Soup2 can be created. The Soup2 class object is created as a static private member of soup2. therefore, there is only one object, and it cannot be accessed unless it is accessed through the public method.

 

In addition, some restrictions are worth noting:

1. Each compilation unit can have only one public class.

2. the name of the public class must completely match the file name containing the compilation unit, including the case.

3. If there is no public class in the compilation unit, you can name the file as needed.

 

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.