"Java Programming Thought-learning Note (ii)" Access control-java access modifier words

Source: Internet
Author: User
Tags modifiers

If you do not provide any access modifiers, it means "package access permissions."
2.1 Package access rights
The package access permissions give the classes within the package access to each other's members.
It should be said that package access provides meaning and justification for the behavior of grouping classes together, that is, the purpose of building a package is not only to classify and differentiate, but also to be able to have each other's code for classes within the same package.
To obtain access to a member:
1) The member's access modifier is public.
2) A way to modify the word without access and place the target class within the same package.
3) Inheritance. Subclasses can access members of the public and protected modifiers of the parent class, but only members of the package access permission can be accessed if the parent-child class is in the same package.
4) Setter and Getter.
2.2 Public: Interface access rights
Public members declare that they are available for each class.
Note: When two is not associated but all are declaring the package, Java is treated as two classes that are subordinate to the default packages and therefore can be accessed from one another.
So, Java is a mandatory logical organization of all execution units.
2.3 Private: You cannot access
Private members cannot be accessed by any other class of the class other than that member.
Because package access permissions cannot be accessed by client programmers, consider setting permissions for members as follows:
1) Use public keywords to explicitly expose members that are used by client programmers.
2) Use private keywords only if you want to work on your own and don't want other people to use or influence other actions.
3) Forget to add any access control when you can automatically get permissions.
Instance code:

Class Do {    privateDo () {}    static Do makeDo() {        returnnew Do();    }}publicclass Use {    publicstaticvoidmain(String[] args) {        do = makeDo();       }}

Purpose of using private:
1) Controls how objects are created and prevents others from creating illegal objects directly using the constructor.
2) The only constructor is private and will hinder the inheritance of other classes to this class.
2.4 Protected: Inheriting access rights
The permission modifier for the inherited invention.
Only classes and subclasses within the same package can access the members of the protected adornment.

"Java Programming Thought-learning Note (ii)" Access control-java access modifier words

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.