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)