Java Access control permissions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.