Detailed explanation of Java road (5) access control

  • 2020-05-19 04:59:32
  • OfStack

In Java, everything has some form of access control.

The control levels of access rights are: public, protected, package access (no keywords), and private, in order from maximum to minimum.

The public, protected, and private access modifiers are used before the definition of each member (domain or method) of the class.

1. Access rights of class members

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

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

2). Give package access rights to a member by not adding the modifier of access rights and placing other classes in the same package, so that other classes in the package can access the member;

3). An inherited class can access either an public member or an protected member.

4). Provide accessor and mutator methods to read and change values.

1. Package access rights

The default access does not have any keywords, but by refers 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 groups all the relevant classes within a package so that they can interact easily with each other.

Note: if two classes are in the same directory and don't give themselves any package names, Java automatically treats such files as belonging to the default package in that directory, and these files have package access to each other.

The following example illustrates the problem:


// class Cake and Pie At the same 1 Directory that is 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 rights

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

3.private: you can't access it

The keyword private indicates that this member cannot be accessed by any class other than the class that contains it. Other classes in the same package do not have access to the private members of this class, so they are isolating themselves.

This use of the private keyword has many USES, such as controlling how objects are created and preventing others from directly accessing a particular constructor (or all of them). see

Here's an 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 Sundae objects by calling the makeASundae() method, but not through the constructor.

This is also true for the private domain in the class.

Just because an object in a class has an private reference does not mean that another object cannot have an public reference to that object.

4.protected: inherited access rights

If a new package is created and classes are inherited from another package, the only member that 1 can access is the public member of the source package.

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

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

2. Interface and implementation

The control of access rights is often referred to as implementation-specific hiding.

Wrapping data and methods into a class, as well as hiding the implementation, is often referred to as encapsulation.

Access control delimits permission boundaries within data types for two important reasons:

1. Set boundaries that client programmers can and cannot use. You can build your own internal mechanisms into the structure without worrying that client programmers will accidentally treat the internal mechanisms as part of the interface they use.

2. Separate the interface from the concrete implementation.

3. Class access rights

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

The modifier must precede the keyword class. Such as:


public class Widget{......}

or


improt access.Widget;

Remember, a class cannot be private (if it is private, then no other class can access it), nor can it be protected (actually an inner class can be private or protected, but this is a special case, described in a later article), only package access or public.

If you don't want anyone else to access the class, you can specify all constructors of the class as private, preventing anyone from creating objects for the class. There are exceptions, however, and this does not prevent you from creating the class within the static members of the class. Let's look at the following example:


class Soup1{
  private Soup1(){}
  public static Soup1 makeSoup(){ // Create objects using static methods 
    return new Soup1();
  }
}

class Soup2{
  private Soup2(){}
  private static Soup2 ps1 = new Soup2(); // Create objects using the singleton pattern 
  public static Soup2 access(){
    return ps1;
  }
  public void f(){}
}

public class Lunch {
  void testPrivate(){
  //Soup1 soup = new Soup1;  Cannot perform 
  }
  void testSingleton(){
    Soup2.access().f();
  }
}


As you can see, the constructors of Soup1 and Soup2 are private, and no one can directly use the constructor to create objects of this class. But we can also use these two classes: create an static method in Soup1, where the constructor is used to create an Soup1 object and return a reference to it; The creation of Soup2 USES the singleton pattern from the design pattern, and only one of its objects can be created. Objects of the Soup2 class are created as one static private member of Soup2, so there is only one, and it cannot be accessed unless through the public method access().

In addition, some limitations are also worth noting:

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

2. The name of the public class must exactly match the filename containing the name given to the compilation unit, including case.

3. If there is no class with public in the compilation unit, you can name the file however you like.


Related articles: