[Java entry notes] three features of object-oriented: encapsulation and java
What is encapsulation?
Java object-oriented programming has three basic features: encapsulation, inheritance, and polymorphism. First, let's take a look at the encapsulation:
In Java's object-oriented programming, Encapsulation refers to a method to package and hide function implementation details. Encapsulation can be considered as a protection barrier to prevent code and data of this class from being freely accessed by code defined by external classes. Strict control is required for code and data in the lifecycle class.
Why use encapsulation?
Encapsulation has the following benefits:
- It can hide the implementation details of some functions in this class. Let the caller implement the functions and data of the callback class through a good method in advance, and restrict unreasonable access to the data in the class.
- Checking data helps to protect the integrity and rationality of object information.
- It facilitates code modification and maintenance and improves the maintainability of the Code.
How to use encapsulation? Access control operator
Java has a total of four access control operators used to encapsulate the data of classes and objects:
Modifier |
Class |
Same package |
Subclass truth |
Global range |
Private |
√ |
|
|
|
Default |
√ |
√ |
|
|
Protected |
√ |
√ |
√ |
|
Public |
√ |
√ |
√ |
√ |
You can also use modifiers for external classes. However, only public and default modifiers can be used for external classes.
Example
Public class Person {// modified with a private modifier, and private String name cannot be accessed externally; // name private int age; // age private String idNum; // ID card number // get the name of this Class Object public String getName () {return name;} // set the name of this class object. If a null value is input, public void setName (String name) {if (name. length () = 0 | name = null) {return;} this. name = name ;}// get the age public int getAge () {return age ;}// set the age. When the input parameter is greater than or equal to 0, set the parameter to the public void setAge (int age) {if (age> = 0) {this. age = age ;}/// obtain the ID card number public String getIdNum () {return idNum;} // set the ID card number public void setIdNum (String idNum) {this. idNum = idNum ;}}
In the above example, we add a private modifier for the attributes in this class, and the external can no longer directly access and modify these attributes. If necessary, we need to add the getXxx and setXxx methods to access these items. At the same time, we can also control the access to these properties in these methods. For example, the name cannot be blank, the age cannot be negative.
In this way, we can also reasonably control the integrity and rationality of the program, enhance the code security, and make the Code better managed and maintained.
Public class Test {public static void main (String [] args) {Person p = new Person (); p. name = "Zhang San"; // error, cannot directly access p. setName ("Zhang San"); p. setAge (-5); System. out. println (p. getAge (); // The default output value is 0. Although we passed a value in the previous code, we made control in the setAge method, so the negative number passed in is not assigned to the p object }}
Usage principles
The use of access control operators should comply with the following principles as much as possible:
- The private modifier should be used for most member variables of the class. Only static and global fields are considered to use the public modifier.
- If a class is mainly used as the parent class of other classes, most of the methods and attributes in the class want to be overwritten and used by the quilt class, and do not want to be directly called by the outside, the protected modifier should be used.
- Public modifiers should be used to expose methods that are directly used by external users.
- The private modifier should be used only when the function of a method is called inside the class.