What are access modifiers in Java EE

Access modifiers are always used when writing Java code. So what's the difference between the four access modifiers? When is the appropriate modifier used? Today, learn more about the difference between these four modifiers.
Four Java modifiers: public, protected, default, private. In this order, the scope of the modifier can be reduced one after the other. To put it in a table:
To check the form's correctness and better understanding, let's test it with code.
First, let's check that all four modifiers can play a role in the same category.

This shows that the four modifiers can all play the same role.
In the same package (subclasses and other classes):
Subclass:

We can see that there is no test4 () method of the parent class in the subclass. Therefore, private cannot be applied to subclasses. What happens if we set up a s.test4 ()?


This also confirms the conclusion given in the table that private cannot work in subclasses of the same package.
Code test:

Non-subclass:

We can see that there is no inheritance from the parent class in the same package, meaning it is not a subclass of it, and private cannot work either.
Code test:

In another package (subclass and other classes):
The following describes the situation in the same package. Next, let's test the effects of the four modifiers in different packages.
The first is the subclass in different packages:

We can see that in various packages only the public and protected subclasses of the class can have an effect.

What happens in other packages besides non-subclasses?

OK, the result is already obvious.
Code test:

OK, except for the public, the other three are all GG.
By demonstrating the code, the different areas of the four access modifiers were shown. I think it's better to understand these four modifiers.


Until this year's Spring Festival, insist on writing at least three blogs a week, knowledge of JavaSE, JavaEE, computer network or data structure. Adding a small tail can be considered to help crowd yourself.
If you come around and find it useful, you might want to give it a thumbs up. Look at that thumbs up, it's long and wide ~~~~~~>