Explain Java access control modifiers in detail with code

Explain Java access control modifiers in detail with code

Java access control character is a very basic content, and there are many articles introduced on the Internet, but many of them are unclear, which caused me a lot of confusion, so I decided to write a blog by myself, combined with the code, for the default Let's make a clear introduction to the two more difficult access control modifiers protected.


foreword

Let's first look at a picture:
write picture description here
    this is a very classic picture of the introduction of access permissions, but there are many places in the description of this picture that puzzle me. For example, regarding the description of default, why can the same package work, but not the subclass?
    Since there was a lot of confusion, and most of the blogs I found on the Internet didn't fully explain my confusion, I decided to write my own code to really understand these access control modifiers.


public&private

    First of all, about these two modifiers, I think it is still well understood:
    public can be accessed no matter where, while private can only be accessed in this class. I won't go into too much detail in this blog.


default

    As can be seen from the table, the default access rights are: the current class and the same package can, but subclasses and other packages cannot. The key here is this: subclasses here represent subclasses located under other packages! If it is simply understood as a subclass literally, it will cause a lot of trouble. . .
    Next, let's find out through the code.

    First, we define a variable value in class A under pk1 without any modifiers, namely default:
    write picture description here
    Next, we create a subclass A1 of class A under the same package and try to access value:
    write picture description here
    you can see , is accessible .

    Then, also under pk1, we create a new common class Main, and try to access the value of class A:
    write picture description here
    there is no problem, it can be accessed .

    Then, we create a new subclass A2 of class A under another package pk2 and try to access the value:
    write picture description here
    an error is reported, and the access fails !

    Finally, we create a new common class under the package pk2, and try to access the value:
    write picture description here
    Similarly, it is also marked with an error, and the access fails !

    To sum up, when we understand default, we don’t need to worry about subclasses and not subclasses. Just remember that if default is used, only those located in the same package have access rights, and the classes located in other packages are all There is no access right!


protected

    I think this modifier is the most difficult to understand among the four modifiers, because it has many "wonderful" performances, let me analyze it in detail with the help of code.

    First, I simply modified the base class A used in the previous part, changed the value to private and added a protected method getValue():
    write picture description here

    Next, try to access the getValue() method in A1:
    write picture description here
    no problem, the access is successful !

    Then, try to access the getValue() method in the pk1.Main class:
    write picture description here
    there is also no problem, the access is successful !

    Then, the magic happens, we try to access the getValue() method in two ways in the subclass A2 of A under pk2:
    write picture description here
    method 1 fails and method 2 succeeds .

    Finally, in the next common class Main of pk2, there are also two methods to try to access the getValue() method:
    write picture description here
    method 1 and method 2 both fail! ! All! ! ! Failed.

    Since the access rights of protected are wider than default, there is no problem with access under the same package, which is not unexpected.
    Fantastic is a comparison of what happens in the pk2.A2 class and the pk2.Main class.
    In class A2, I use the instance of A to access getValue(), which is not allowed, but it is allowed to use the instance of A2 to access getValue() (that is, method 2. Call getValue() directly).
    In the class pk2.Main, I also use the two methods in A2, method 1 fails as expected, but method 2 also fails! This caused me a lot of confusion. I checked a lot of information and finally found a reasonable explanation :
    for the protected modifier, under the same package, it is actually equivalent to default, and access is completely allowed. However, when in other packages, no matter what class, it is not allowed to access this method by declaring an instance of this protected method (in this case, instance a of A). For subclasses under different packages (in this article, A2), without overriding and overloading a protected method, the protected method can only be accessed in this class (because of inheritance, A2 actually hides the protected method). Inherited this protected method), and out of this class, the implicitly inherited protected method will become invisible (in this class, corresponding to pk2.Main), so, in pk2.Main Method 2 also fails.

    Then someone must have doubts, doesn't A2 inherit this protected method? Why does pk2.Main have no access rights? Isn't it located in the same package as A2? This is because when protected is out of its own package, its meaning becomes "visible only to subclasses" (this is how protected is reflected), so only in subclasses (A2) have permission to access This protected method!

    So I have to access the getValue() method through A2 in pk2.Main? of course can! Just need to explicitly rewrite this protected method in A2. The modified code is as follows:
    write picture description here
    write picture description here
    Method 2 successfully accessed! Sprinkle flowers~~~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325484579&siteId=291194637