There are 4 types of access modifiers in the Java language

Source: Internet
Author: User
Tags modifiers

Reprint: http://wuhaidong.iteye.com/blog/851754

There are 4 types of access modifiers in the Java language

There are 4 access modifiers in the Java language: package (default), private, public, and protected.

1. The package is the default protected mode, plus a packet access, which is used when there are no modifiers. Package access allows domains and methods to be accessed by any method of any class within the same package. (In-package access).

2. Private identifies access patterns that represent private domains and methods that can only be accessed by other methods in the same class, implement data hiding, and, if necessary, access private variables through methods. (In-class access).

3. The public modifier is used to expose domains and methods so that they can be accessed outside of the package defined by the class. This level is also required for the necessary interface elements in the package and class, and the main () method must be public, and the ToString () method must also be public. It is not common to expose a domain with public unless the domain has been declared final. (Cross-package access).

4. The protected modifier provides a way to access the package (with restrictions) from outside the package. Adding protected modifiers before fields and methods does not affect access to them by methods of other classes within the same package. To access the package from outside the package, which contains the protected member's class, you must ensure that the class being accessed is a subclass with the protected member class. That is, you can use this level when you want a class in the package to be reused by classes outside of the package. Should be used with caution in general. (The class in the package is used with caution in the outer class inheritance).

There are 4 types of access modifiers in the Java language

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.