Java Object-oriented encapsulation is achieved through access control of member variables and methods, and access control is divided into 4 levels: private, default, protected, and public, as follows:
1. Private level
Private-level keywords are private, and private-level member variables and methods can be used only within the class in which they reside, and direct access is not allowed in other classes. The private level is the most restrictive. The sample code is as follows:
1 //Privateclass.java File2 Public classPrivateclass {3 Private intx; 4 5 PublicPrivateclass () {6x = 100;7 }8 Private voidPrintx () {9System.out.println ("Value of X is" +x);Ten } One } A - - //Helloworld.java file Call Privateclass the Public classHelloWorld { - Public Static voidMain (string[] args) { - - Privateclass p; +p =NewPrivateclass (); - + //compile error, Method Printx () not visible in Privateclass A P.printx (); at } -}
the 2nd line of the preceding code declares the Privateclass class, where the code line 3rd is the declaration private instance variable x, and the code 5th line is the construction method that declares the public. The code line 8th declares a private instance method. The code 22nd line in the HelloWorld class has a compile error because the method of Printx () in Privateclass is a private method.
2. Default Level
The default level has no keywords, that is, no access modifiers, and the default level of member variables and methods can be accessed directly within their class and other classes in the same package, but not directly in the classes of different packages. The sample code is as follows:
1 //Defaultclass.java File2 Packagecom. Kevin;3 4 Public classDefaultclass {5 6 intx; 7 8 PublicDefaultclass () {9x = 100;Ten } One A voidPrintx () { -System.out.println ("Value of X is" +x); - } the -}
There is no access restriction modifier before the x variable in line 6th of the preceding code, and the method for line 12th is also without an access restriction modifier. Their access levels have a default access level.
In the same package (COM. Kevin) Call the Defaultclass class code as follows:
1 //com. Kevin Package in Helloworld.java file2 Packagecom. Kevin;3 4 Public classHelloWorld {5 6 Public Static voidMain (string[] args) {7 8 Defaultclass p;9p =NewDefaultclass ();Ten P.printx (); One } A}
The default access level can be accessed in the same package, and the code above can be compiled through.
Calling the Defaultclass class code in a different package is as follows:
1 //Helloworld.java file in default package2 Importcom. Kevin.defaultclass;3 4 Public classHelloWorld {5 6 Public Static voidMain (string[] args) {7 8 Defaultclass p;9p =NewDefaultclass ();Ten //compile error, Method Printx () not visible in Defaultclass One P.printx (); A } -}
The Helloworld.java file is not in the same package as the Defaultclass class, Printx () is the default access level, so the P.printx () method cannot be compiled.
3. Total level
Public-level keywords are publicly, and public-level member variables and methods can be accessed directly in any situation and are the most permissive level of access control. The sample code is as follows:
1 //Publicclass.java File2 Packagecom. Kevin;3 4 Public classPublicclass {5 6 Public intx; 7 8 PublicPublicclass () {9x = 100;Ten } One A Public voidPrintx () { -System.out.println ("Value of X is" +x); - } the -}
The x variable in line 6th of the preceding code is the public level, and the method of line 12th is the public level. Call the Publicclass class code as follows:
1 //Helloworld.java file in default package2 Importcom. Kevin.publicclass;3 4 Public classHelloWorld {5 6 Public Static voidMain (string[] args) {7 8 Publicclass p;9p =NewPublicclass ();Ten P.printx (); One } A}
The Helloworld.java file is not in the same package as the Publicclass class and provides direct access to the public Printx () method.
4. Protection level
The protection level keyword is protected, and the protection level is exactly the same as the default access level in the same package, but different packets of sub-classes can inherit the protected variables and methods in the parent class, which is known as the protection level, which is the protection of a class's subclasses can inherit the class's variables and methods. The sample code is as follows:
1 //Protectedclass.java File2 Packagecom. Kevin;3 4 Public classProtectedclass {5 6 protected intx; 7 8 PublicProtectedclass () {9x = 100;Ten } One A protected voidPrintx () { -System.out.println ("Value of X is" +x); - } the -}
The x variable in line 6th of the above code is the protection level, and the method of line 12th is the protection level.
In the same package (COM. Kevin) Call the Protectedclass class code as follows:
1 //Helloworld.java file in default package2 Packagecom. Kevin;3 4 Importcom. Kevin.protectedclass;5 6 Public classHelloWorld {7 8 Public Static voidMain (string[] args) {9 Ten Protectedclass p; Onep =NewProtectedclass (); A //methods in Protectedclass can be accessed directly in the same package printx () - P.printx (); - the } -}
Protected access level in the same package as the default access level, you can directly access the Protectedclass Printx () method, which can be compiled through.
Calling the Protectedclass class code in a different package is as follows:
1 //Helloworld.java file in default package2 Importcom. Kevin.protectedclass;3 4 Public classHelloWorld {5 6 Public Static voidMain (string[] args) {7 8 Protectedclass p;9p =NewProtectedclass ();Ten //compile error, no direct access to protection method in different packages Printx () One P.printx (); A - } -}
The Helloworld.java file is not in the same package as the Protectedclass class, and the Protection Method Printx () cannot be accessed directly in different packages, so the P.printx () method cannot be compiled.
Inherit the Protectedclass class code in a different package as follows:
1 //Subclass.java file in default package2 Importcom. Kevin.protectedclass;3 4 Public classSubclassextendsProtectedclass {5 6 voiddisplay () {7 //the Printx () method inherits from the parent class.8 Printx (); 9 //The x instance variable is inherited from the parent class.Ten System.out.println (x); One } A}
In different packages, subclass inherits the Printx () method and the X instance variable from the Protectedclass class. Line 8th of the code is to invoke the method inherited from the parent class, and line 10th is called an instance variable that inherits from the parent class.
There are two ways to access a member: A call that calls its members through a class or object, such as a p.printx () statement, and an inheritance, that is, the subclass inherits the member variables and methods of the parent class.
-
Public access level in any case both ways are possible;
-
The default access level in the same package two access methods can not be accessed outside the package;
-
Protect access levels in the same package as the default access level, both access methods are available. However, the access can only be inherited from different packages;
-
Private access levels can only be accessed by invoking methods in this class and cannot inherit access.
When you are prompted to access a class member, you should try to limit the visibility of the members in the class, with the access level order: private level → default level → protection level → public level, if you are satisfied with the usage.
[Java Learning Basics] The encapsulation and access control of Java