Java scope: Public, private, protected, and no-write differences

Source: Internet
Author: User

Before explaining the Four keywords, I want Class Make a simple definition of the relationship between them. Class , Base Class It can be thought that they are all their children, and Classes And think they are all friends of their own.

 

1,Public:PublicIndicates that the data member and member function are open to all users, and all users can directly call the function.

 

2,Private:PrivatePrivate means thatClassNo one except yourself can use it directly. Private property cannot be infringed. Even children and friends cannot use it.

 

3 , Protected : Protected For children and friends Public Can be used freely, without any restrictions, and for other external Class , Protected It becomes Private .

Scope: current class, same package, Child class, other package

Public √

Protected √ ×

Friendly √ ××

Private √ ×××

The default value is friendly when no data is written.

 

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.