There are altogether 4 access control permissions in Java, private, default, protected, public
1. Private access rights
Private access, which can be used in the definition of attributes, the declaration of methods, once the Private keyword declaration is used, can only be accessed in this class
2. Default access rights
If a property or method in a class does not use any of the access rights claims, it is the default access permission, which can be accessed by other classes in the package, but not by the classes of other packages
3. Protected access rights
Protected is a protected access, members of a class can only be accessed by this package and by subclasses of different packages if protected access is used
4. Public access rights
Public is publicly accessible and can be accessed in all classes, whether or not the same package is used, if members of a class
Packagecom.test;/** * @authorYyx June 29, 2017*/ Public classPerson {PrivateString name; protected intAge ; PublicString sex; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; }} Packagecom.example;ImportCom.test.Person;/** * @authorYyx * June 29, 2017*/ Public classStudentextendsPerson { Public Static voidMain (string[] args) {Student stu=NewStudent (); System.out.println (Stu.age); System.out.println (Stu.sex); System.out.println (stu.name);//Error }}
Java Access control permissions