Understanding Java's encapsulation and interface

Source: Internet
Author: User

1, encapsulation, that is, to retain a limited external interface (interface), hide specific implementation details.

2, encapsulation in life is very common. For example, here is a charging torch:

A user can guess the operation of the flashlight even if he doesn't read the instructions: switch and charge. The torch is a plastic shell that hides the internal details that the user does not need to touch, leaving only two connectors, switches and electrical plugs. With these two interfaces, the user is sufficient to use the functionality that the product wants to implement in the design. If all the details are exposed to the user at the same time, the user will feel overwhelmed with the product (e.g. a remote control without shell below). As a result, encapsulation improves the ease of use of the product.

If the product is not encapsulated, many details of the flashlight or remote control will be exposed to the user: battery, circuit, sealed rubber and so on. While this allows the user to operate the product more freely, such as discharging the battery directly, removing an LED lamp, etc. However, users often have to bear the risk of greater damage to the product. As a result, the package improves product safety.

3. A Java software PRODUCT is the same as a daily product. An object can have many members (data members and methods) inside it. There are some data members and methods that are only used internally. At this point, we would like to have a mechanism to "shell" the object, thus encapsulating the object. This makes it easier for users to learn and use external interfaces without having to touch internal members.

4. Java uses three keywords to control the external visibility of members of an object (visibility): public, private, protected for encapsulation.

  • Public: The member is visible externally, that is, the member is part of the interface
  • Private: The member is not visible externally and can only be used for internal use and cannot be accessed externally.

Understanding Java's encapsulation and interface

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.