What is the difference between public private protected in Java and non-write?

Source: Internet
Author: User
Before explaining the Four keywords, I want to make a simple definition of the relationship between classes. For a class that inherits its own, the base class can think that they are all their children, classes in the same directory as yourself are considered to be your friends.

1. Public: indicates that the data member and member function are open to all users and can be directly called by all users.

2. Private: Private indicates private. Private means that no one except the class itself can directly use private property. Private property cannot be infringed, even if it is a child or friend, none.

3. Protected: for children and friends, protected is public and can be used freely without any restrictions. For other external classes, protected becomes private.

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

Public √

Protected √ ×

Friendly √ ××

Private √ ×××

Note: friendly is used by default when no data is written.

Related Article

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.