Java learning notes (class visibility)

Source: Internet
Author: User

1) Class visibility: public; friendly (without keywords); protected; private;
Public: public class, which is visible inside and outside the package and has the highest visibility level;
Friendly: it is also the default security level, which is invisible outside the package and visible in the package. It is used in the same internal class as the protected level.
Protected: protection level. It can only be used for internal classes. It can be visible to classes and subclasses (subclass inside the package, but not outside the package) and other classes in the package.
PRIVATE: private level. It can only be used for internal classes and is only visible to the class itself.
The visibility of the class is still relatively complex, especially after the internal class is nested, but the basic rules are described above. Therefore, it is recommended that if the class is nested when writing,
No more than two layers, that is, only one layer is nested. This process is relatively simple, and there is no need for multiple visibility nesting to complicate things.
The following is an exampleCode:

 
Public class myclass02 {// the protection level is visible to the subclass and friendly class in the same package. The classes under protected class myclass03 {} // are only visible to this class and cannot be accessed outside the class. private class myclass04 {}// same as protected. class myclass05 {}// visible to the subclass, inside the package, and other classes outside the package. public class myclass06 {}} class myclass021 extends myclass02 {export Thea = new myclass03 (); myclass05 the05 = new myclass05 ();} class myclass11 {myclass03 Thea = new myclass02 (). new myclass03 ();}
// Located in another package. this class inherits from myclass02class myclass022 extends myclass02 {public Void Do () {// myclass03, myclass05 invisible, the following statement compilation error. // myclass03 Thea = new myclass03 (); // myclass05 the05 = new myclass05 (); // The following statement is acceptable. myclass06 the6 = new myclass06 ();}}

2) class member visibility:
Public: public members. Other classes are visible (inside the package and outside the package), and classes outside the package are visible. Internal Nested classes are visible;
Protected: protection level. Other classes in the package are visible, and subclasses inside and outside the package are visible. Nested classes are visible;
No keywords: friendly, other classes in the package are visible, but the subclass inside and outside the package is invisible; the nested classes are visible; there is no inheritance.
PRIVATE: Private. Other classes cannot be accessed, but they are visible to internal classes of the class;
In addition, the internal class can be defined in the class method, and the visibility is closely related to the scope. Generally
We recommend that you do not use classes like this, which will increase the difficulty of maintenance and understanding.

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.