Access control permission

Source: Internet
Author: User
Access control permission

The maximum and minimum permissions are: public, protected, package access permission (no keyword), and private.

The default access permission does not have any keywords, but generally refers to the package access permission. This means that all other classes in the current package have access permissions to that member, but this member is private for all classes outside the package. Since a compilation unit can only belong to one package, all classes in the same compilation unit are automatically accessible to each other through package access permissions. The package access permission allows you to combine all the related classes in the package so that they can interact easily.

Public: interface access permission. When the public keyword is used, it means that the members following the public statement are available to everyone.

PRIVATE: you cannot access it. The private keyword indicates that, except for the class containing this member, no other class can access this member. For this reason, private allows you to change the member at will without considering whether this will affect other members in the package.

import java.util.*;class Sundae{private Sundae(){}static Sundae makeSundae(){return new Sundae();}void display(){System.out.println("This is Sundae!");}}class Sundae2{private Sundae2(){}private static Sundae2 s=new Sundae2();public static Sundae2 access(){return s;}void display(){System.out.println("This is Sundae2!");}}public class IceCream{public static void main(String[] args){//Sundae=new Sundae();Sundae x=Sundae.makeSundae();x.display();Sundae2 y=Sundae2.access();y.display();}}

You can control how to create objects and prevent others from directly accessing a specific constructor.

Protected: inherits the access permission. The creator of the base class will want to have a specific member and grant its access permission to the derived class instead of all classes. This requires protected to complete this work. Protected also provides package access permissions. Other classes in the same package can access the protected element.

The class cannot be private or protected. Therefore, the class access permission has only two options: Package access permission and public. If you do not want anyone else to have access to this class, you can specify all constructors as private, except for the internal creation of static members of this class.

There are two reasons to control access permissions for members:

1. In order to prevent users from touching the parts they should not touch.

2. In order for the class library designer to change the internal working mode of the class, there is no need to worry that this will have a major impact on the client programmer.


Access control permission

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.