Detailed Java Road (v) Access rights control _java

Source: Internet
Author: User
Tags modifiers

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

Access to the control level from the largest to the smallest sequence is: public,protected, packet access (no keywords) and private.

Public,protected and private These Java access modifiers, when used, are placed before each member (domain or method) in the class.

First, the access rights of class members

The only way to gain access to a member is to:

1). Make the member public. Whoever is there can access the member;

2. The other classes in the package can access the member by giving the package access to the member in a way that does not have access to the modifier and places the other class in the same package;

3. The inherited class can access both the public member and the protected member.

4. Provide accessor and mutation methods to read and change values.

1. Package access rights

The default access rights do not have any keywords, but they refer to package access, which means that all other classes in the current report have access to that member, but for all classes outside the package, the member is private.

Package access allows all related classes in the package to be grouped together so that they interact easily with each other.

Note: If two classes are in the same directory and do not set any package names for themselves, Java will automatically think of such files as belonging to the default package for that directory, so that they have package access to each other.

The following example illustrates the problem:

Class 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 ();
   }

Output is pie.f ()

2.public: Interface access rights

Using the keyword public means that subsequent member declarations are available to everyone, especially the client programmer who uses the class library.

3.private: You can't access

The keyword private represents the class that contains the member, and no other class can access the member. Other classes within the same package cannot be accessed by private members of this class, so this is the equivalent of isolating yourself.

This role of private keywords has many uses, such as controlling how objects are created, and preventing people from accessing a particular constructor (or all constructors) directly. 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 create a Sundae object by calling the Makeasundae () method, but not through the constructor.

This is also true for private domains in a class.

Note, however, that you cannot assume that other objects cannot own public references to an object in a class because the reference to it is private.

4.protected: Inheriting access rights

If you create a new package and inherit the class from another package, the only member that can be accessed is the public member of the source package.

Sometimes the creator of a base class wants to give the access rights of a particular member to a derived class instead of to all classes, which requires the use of keyword protected.

Note that protected also provides package access, that is, other classes within the same package can also access protected elements of this class.

Second, interface and implementation

The control of access rights is often referred to as the concealment of specific implementations.

The data and methods are packaged into classes, and the specific implementations are hidden, often collectively referred to as encapsulation.

For two important reasons, access control delimits the bounds of the permission within the data type:

1. To set limits that can be used and not used by client programmers. You can build your own internal mechanism in the structure without worrying that the client programmer will accidentally use the internal mechanism as part of the interface they are using.

2. The interface and the concrete implementation are separated.

Third, the class's access rights

In Java, access modifiers can also be used to determine which classes in a library are available to consumers of the library.

Modifiers must be placed before the keyword class. For example:

public class widget{...}

Or

Improt access. Widget;

You know, a class cannot be private (if the class is private, no class can access it except that class), nor can it be protected (in fact, an inner class can be private or protected, but this is a special case, Described in subsequent articles, can only be package access or public.

If you do not want other people to access the class, you can designate all constructors for that class as private, preventing anyone from creating objects of that class. But there are exceptions, and this does not prevent you from creating the class within the static members of the class. Let's look at the example below:

Class soup1{
  Private Soup1 () {} public
  static Soup1 Makesoup () {//Create object with static method return
    new Soup1 ();
  }

class soup2{
  private Soup2 () {}
  private static Soup2 PS1 = new Soup2 ();
  Access () {return
    PS1;
  }
  public void F () {}
} is public

class lunch {
  void testprivate () {
  //soup1 soup = new Soup1; cannot execute
  }
   void Testsingleton () {
    soup2.access (). f ();
  }
}


As we can see, the constructors for the Soup1 and Soup2 classes are private, and no one is allowed to use the constructor directly to create objects of that class. But we can also use these two classes: Create a static method in Soup1, use the constructor in this method to create a Soup1 object and return its reference; Soup2 was created with a single case pattern in design mode, and only one object of it was created. The object of the Soup2 class is created as a static private member of the SOUP2, so there is only one and there is no access to it except through the public method access ().

In addition, some restrictions are noteworthy:

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

The name of the 2.public class must match exactly the name of the file that contains the compilation unit, including capitalization.

3. If there are no classes with public in the compilation unit, you can name the files randomly.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.