Java Object-Oriented learning note--2 (access control modifier)

Source: Internet
Author: User

1. Access control modifiers (encapsulation)

Encapsulation: Encapsulates the data inside a class and encapsulates the algorithm into a method.

1) Packaging principle: will not need to provide external content is hidden, the properties are hidden, provide public methods to access them, there are usually two ways to access: Set settings, get get.

2) package result: present but not visible.

3) Public: Any location visible, can be decorated: class, member property, member method, inner class, cross-package access class (need to import using an import statement), member property = member variable.

4) Protected: visible in the current package, visible in subclasses. Can be decorated: member properties, member methods, inner classes (can only be used in the class body, cannot be decorated with classes).

5) Default: The current package is visible inside, there is no modifier, can be modified: class, member property, member method, inner class, but rarely used in actual projects. Access scope of the default class (in-Package Class): The current package is internally visible, the class cannot be accessed in another package, access is restricted! If the main method is set in the default class, the JVM will not be found and cannot be executed, so it must be in the public class.

6) Private: Only visible within the class. Can be decorated: member properties, member methods, inner classes (can only be used in the class body, cannot be decorated with classes). Private methods cannot inherit, nor can they be overridden.

Recommended in the project: All classes are public classes. The encapsulated class uses the inner class!


This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1654842

Java Object-Oriented learning note--2 (access control modifier)

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.