Access control is also known as “hidden implementation,” which means that we can use it to determine how visible a class or its members are in a program. For example, elements that are decorated public are visible globally, while elements that are decorated private are visible only inside the class.

Java’s access control provides four different access qualifiers that describe the extent to which an element is visible in a program. Let’s take a look at it in detail:

The concept of the package

Java is the most basic file units in class, each filename is not allowed to repeat the class files, to ensure that the only certainty, but in Java have tens of thousands of classes, each class will need to define a new and before all the different types of the name of the class, must have just find a not repetitive and a bit of a symbolic name will have U.S..

Java introduced a package management mechanism to solve this problem. Some classes are grouped together into a “folder”, so that classes with the same class name in different folders do not constitute a conflict, greatly reducing the probability of class name conflict. And the so-called “folder” here is officially a package.

Each class should be under a package, and each time a new class is created, the system will search for classes with the same name in the current package, rather than the entire classpath.

With packages in place, the positioning and reference of classes change and are typically uniquely identified using fully qualified class names. For example: java.lang.String, java.util.Date, etc

There is no explicit constraint on package naming, just an unwritten convention that package names are usually formed in reverse order of a domain name. For example, com.baidu.Test, net.csdn.date and so on. Since generic domain names are unique, naming packages using domain names is a really good strategy.

So the import function is simply to omit the package name and reduce unnecessary duplication. If you use both java.util.Date and java.sql.Date in a Java file, then import only applies to one of them, and you need the full class name for the other.

Class access modifier

For classes, Java allows only two access qualifiers to be modified. (Except inner classes)

  • Public: a class decorated by public that is visible anywhere in the world
  • Default modifiers: There is no specific keyword for the default modifier. If you do not have a modifier before the Class, use the default modifier. This modifier specifies that the Class is visible in the package scope

Such as:

// Create a class under the package Class_AccessModefier. public class PublicClass { }Copy the code
// Other locations of the same package are the package Class_AccessModefier that can be accessed; public class Test { public static void main(String[] args){ PublicClass publicClass = new PublicClass(); }}Copy the code
// Other locations under different packages are also accessible package Others; import Class_AccessModefier.PublicClass; public class Test { public static void main(String[] args){ PublicClass publicClass = new PublicClass(); }}Copy the code

Obviously, once a class is declared public, it will be visible anywhere in the entire Java program.

Here’s another piece of code:

package Class_AccessModefier;

class DefaultClass {
    
}
Copy the code
// Other locations in the same package are visible to the class as package Class_AccessModefier; public class Test { public static void main(String[] args){ DefaultClass defaultClass = new DefaultClass(); }}Copy the code
// Class package Others cannot be accessed under different packages. Public class Test {public static void main(String[] args){public static void main(String[] args){ }}Copy the code

We are talking about external classes, not inner classes, which only allow access to classes using public and default modifiers. Inner classes are much more general, which we will describe in more detail later.

Access modifier for a class member

Class members include the class’s field properties and methods, for which Java provides four different qualifiers to limit visibility.

  • Public: The method or property decorated by public, with the highest visibility and accessible from anywhere
  • Protected: METHODS or properties that are protected are visible within the package scope and are accessible by subclasses that are not in the same package
  • Default: Default modifiers modify methods or properties that are visible anywhere in the package
  • Private: Externally inaccessible, but internally accessible

Let’s look at some code:

// We define four member attributes with different visibility public class PublicClass {public String name ="hello world";
    protected String sex = "man";
    int age = 23;
    private String tel = "3234234234";
}
Copy the code
Package Member_AccessModefier; public class Test { public static void main(String[] args){ PublicClass publicClass = new PublicClass(); System.out.println(publicClass.name); System.out.println(publicClass.age); System.out.println(publicClass.sex); System.out.println(publicclass.tel); system.out.println (publicclass.tel); }}Copy the code

Properties that public decorates are visible everywhere, needless to say.

The protected sex modifier is also accessible because the PublicClass of our main function is in the same package, so it is naturally accessible

The unmodified age attribute is also accessible because the main function’s PublicClass class is in the same package

The tel property modified by private is not visible, meaning that private does not allow any external access to the property, even if you are a subclass of me

The same code we put in another package will have a different result:

package Others; import Member_AccessModefier.PublicClass; public class Test { public static void main(String[] args){ PublicClass publicClass = new PublicClass(); System.out.println(publicClass.name); Println (publicClass.age); system.out.println (publicclass.age); System.out.println(publicclass.sex); system.out.println (publicclass.sex); System.out.println(publicclass.tel); system.out.println (publicclass.tel); }}Copy the code

Only the name property, which is decorated as public, is visible; nothing else is accessible.

Age is the default modifier, visible to the package, and immediately inaccessible outside the package.

Sex is protected and is not visible because it is outside the scope of the package and does not inherit the target class.

Private Needless to say, no external location is visible.

Here, once our Test class inherits PublicClass, the sex property becomes accessible immediately, so you can try it out.


All the code, images and files in this article are stored in the cloud on my GitHub:

(https://github.com/SingleYam/overview_java)

Welcome to wechat public number: jump on the code of Gorky, all articles will be synchronized in the public number.