Java permission modifiers (public, private, protected, default friendly)

In the past, access modifiers were always ambiguous, and I often couldn’t explain them clearly even if I asked myself to explain them carefully. This time I want to understand them thoroughly.

Now the summary is as follows:

1. Summary of the access rights of each access modifier to different packages and their subclasses, non-subclasses

Java access permission modifiers include four: public, protected, friendly, private; However, friendly does not have explicit declarations, member variables and methods in java The default is friendly permissions.

Now express their access rights: (There are currently two main ways to display access modifiers)

Representation of Figure 1: (The first display method divides different class files into two variables, “whether it is the same package” and “whether it is a subclass”. In this way, there are a total of 2*2=4 situations plus For this type of visit, a total of 5 situations were considered. I am more in favor of this classification method. It is simple, clear and easy to understand.)

Representation Figure 2: (This display method is also good. This display method has one more layer, the access conditions in the same class, but the subclasses and non-subclasses in the same package are unified, and each access condition of Java Modifiers have the same access effect on subclasses and non-subclasses of the same package, and can be combined)

access modifier

same class

Same package

Different packages, subclasses

Different packages, not subclasses

private

default

protected

public

2. First, explain the public permissions issue

Define four classes: Person, People, Student, and Computer, and compare the differences between this class, the same package, subclasses, and other packages respectively.

Note here: When rewriting a parent class member function, the child class member function cannot have less permissions than the parent class member function. Generally, rewriting can define the permissions the same.

The code is shown in the figure:

1.public similar access

2.public access of different classes in the same package (non-subclass)

3.public access to different classes in the same package (subclasses)

4.public non-subclass access under different packages:

5. Public subclass access under different packages:


There are no compilation errors regarding the member variable name above, indicating that there is no problem with the public type in these five situations.

3.Let’s talk about the issue of protected permissions.

Appendix Person.java screenshot

4.default(friendlyly) permission issue.

Also define four classes: Person, People, Student, and Computer, and compare the differences between this class, the same package, subclasses, and other packages respectively.

Note here: default (default is specified without writing) can modify any class like public and the class name must be the same as the file name.

The code of several other classes is the same as in the public test.

Here is only the code and error in one of the errored Students.

If the Person, People, and Teacher classes can be compiled and passed, it means that classes modified with default can access each other in this class and the same package. However, if the Student and Computer classes are not compiled, it means that the default cannot be outside the package, regardless of Classes with or without inheritance relationships cannot access each other.

Five: Final testprivate permission issue,

Private means private. If Person can be compiled and compiled but People, Student, Teacher, and Computer cannot be compiled, it means that the class modified with private can only be accessed in this class. The test results are as follows:

To sum up, the difference and detailed explanation of the access permissions of the four modifiers in Java have been demonstrated. Once again, the rigor and security of the Java language are explained. We will create a new one when using these four modifiers in the future. Class, the permissions of this class should be as small as possible, so as to reduce vulnerabilities and improve security, thus reflecting the object-oriented encapsulation of Java.

Finally, let’s recall the access permissions of the four modifiers in Java as shown in the figure:

================================================== =========

Replenish:

1. Access permission modifier modifies class name:

1. Classes cannot be modified with protected and private. (As you can see in the picture above, the only modifiers that modify the class name are public abstract and final.)
2. A class modified with friendly is called a friendly class. When using a friendly class to create objects in another class, make sure they are in the same package. (As can be seen from the above, everything modified with default is inaccessible outside the package.)

2. Access permission modifiers and inheritance

Access modifiers here refer to modifying member variables and methods. It can be divided into two situations:

1, the subclass and the parent class are in the same package

At this time, only variables and methods declared as private cannot be inherited (accessed).

Copy code

 class Father
     {
       private int money;
       int weight=100;
      
      }
     class Son extends Father
     {
       viod f()
       {
         money=10000; // illegal
         weight=100; // legal
        }
      }

Copy code

2. The subclass and the parent class are not in the same package

At this time, neither private nor friendly can be inherited (accessed), but protected and public can.

Father.java

Copy code

 package com.aaa;
      public class Father
     {
       int height;
       protected int money=120;
       public int weight;
       protected int getMoney()
       {
         return money;
       }
       void setMoney(int newMoney)
       {
         money=newMoney;
       }
      }

Copy code

Son.java

Copy code

 package com.bbb;
      import com.aaa.Father;
      public class Son extends Father
      {
         void f()
         {
           money=10000;//Legal
           //height=170;//Illegal, height is a variable modified by friendly
           System.out.println(money);//The output result is 10000
           //setMoney(300); //Illegal
           int number=getMoney(); //Legal
           System.out.println(number);//The output result is 10000
          }
           public static void main(String args[])
          {
            Son sss=new Son();
            sss.f();
           }
       }

Copy code

Therefore, theaccess permission modifier permissions from high to low are public, protected, friendly, and private.

The inheritance problem above is actually a permissions issue with access modifiers.

1. Public: public indicates that the data members and member functions are open to all users, and all users can call them directly. 2. Protected: Protected is public for children and brothers and can be used freely without any restrictions. For other external classes, protected becomes private. 3. default (friendly): Java’s default access permissions. This permission is usually called package access permissions. Default is public for brothers. Outside the package, these members become Become private.

4. Private: Private means private. Private means that no one can use it directly except the class itself. If other classes want to access it, they can access it through the member methods of the class, such as get/set

Author: SummerChill
Source: http://www.cnblogs.com/DreamDrive/
This blog is a summary of my own or a reprint of technical blog posts found on the Internet. If there are any errors in the article, please point them out. To prevent more people from being misled.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 132204 people are learning the system