Client programmer: A class consumer who uses the data type in its application, his goal is to collect a variety of classes for rapid application development.
Class Creator: A programmer who creates a new data type, with the goal of building the class.
The reason for the existence of access control: A, so that the client programmer can not touch the parts they should not touch; b. Allows the library designer to change how the class works internally without worrying about affecting the client programmer
Java Four keywords: public, protected, default, private (they decide what can be used to define what is immediately followed)
The smaller the scope < scope of access, the higher the security >
Access Rights class Bun class other packages
Public∨∨∨∨ (available to anyone)
Protect∨∨∨x (inherited classes can be accessed and have the same permissions as private)
DEFAULT∨∨XX (Package access, which can be accessed throughout the package)
PRIVATE∨XXX (elements that cannot be accessed by anyone other than the type creator and the internal method of the type)
Below, detailed in code (build four class person Student Teacher parents and a test class)
Package com.zq.demo.test1;
/**
* In-Class
* @author Administrator
*/
public class Person {
Public String uname = "Harry";
public void Introducemyself () {
System.out.println (uname);
}
}
Package com.zq.demo.test1;
Same package
public class Student {
Person p = new person ();
public void Test () {
System.out.println (P.uname);
}
}
Package com.zq.demo.test1;
Sub-class
public class Teacher extends person{
public int age;
Person p = new person ();
public void Test1 () {
System.out.println (P.uname);
}
}
Package com.zq.demo.test2;
Different packages
Import Com.zq.demo.test1.Person;
public class Parents {
Public String uname = "haha";
Person p = new person ();
public void Test2 () {
System.out.println (P.uname);
}
}
The result of the test class shows that public can access the
Test protected with the same four classes
After changing the uanme in person to protected class parents, the field person.uname is not visible (the description cannot span the package)
Three other non-impact
Description In addition to cross-package other does not affect access
Private
Access is available only in this class, but with the set, get method in the person class about uname
Default
Test result description can only be accessed in this package in other packages even if an inheritance relationship is not accessible
Four access modifiers in Java the whole process of distinguishing and elaborating