To summarize the previous content, object refers to a certain thing, and class refers to the type of object. Objects can have states and actions, that is, data members and methods.
Until now, data members and methods have been open to both internal and external users. Inside the object, we use this to call the object's data members and methods. Outside the object, for example when we call the object in another class, we can use object.data members and object.method() to call the object's data members and methods.
We are going to encapsulate the members of the object (members include data members and methods), thereby allowing only some members to be called from the outside. Using encapsulation, we can improve the ease of use and security of objects.
Encapsulation and interface
Encapsulation is a common term for computers, which means retaining a limited external interface and hiding specific implementation details. For example, in the Linux architecture, you can see that the Linux operating system encapsulates the specific details of the underlying hardware, retaining only the system call interface. The user is outside the package and can only perform the required operations through the interface.
Encapsulation is very common in life. For example, here is a rechargeable flashlight:
Even without reading the manual, a user can guess the operation of this flashlight: switching and charging. This flashlight uses a plastic shell to hide the internal details that users do not need to touch, leaving only two interfaces, the switch and the electrical plug. Using these two interfaces, users are enough to use the functions that the product is designed to achieve. If all the details are exposed to the user at the same time, the user will feel overwhelmed by the product (such as the unshelled remote control below). Therefore, encapsulation improves the ease of use of the product.
If the product is not packaged, many details of the flashlight or remote control will be exposed to the user: battery, circuit, sealed rubber, etc. Although this allows users to operate the product more freely, such as directly discharging the battery, taking out an LED light, etc. However, users often bear a greater risk of damaging the product. Therefore, encapsulation improves the safety of the product.
A Java software product is the same as an everyday product. An object can have many members (data members and methods) inside it. Some data members and methods are used internally only. At this time, we will hope to have a mechanism to "pack" the object to encapsulate the object. In this way, users can more easily learn and use external interfaces without having to contact internal members.
Encapsulation of object members
Java uses three keywords to control the external visibility of members of an object: public, private, protected.
1.public: The member is visible externally, that is, the member is part of the interface
2.private: This member is not visible to the outside and can only be used internally and cannot be accessed from the outside.
(protected involves the concept of inheritance, which will be discussed later)
Let’s first encapsulate the previously defined Human class:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human(160);
System.out.println(aPerson.getHeight());
aPerson.growHeight(170);
System.out.println(aPerson.getHeight());
aPerson.repeatBreath(100);
}
}
classHuman
{
/**
* constructor
*/
public Human(int h)
{
this.height = h;
System.out.println("I'm born");
}
/**
*accessor
*/
public int getHeight()
{
return this.height;
}
/**
* mutator
*/
public void growHeight(int h)
{
this.height = this.height + h;
}
/**
* encapsulated, for internal use
*/
private void breath()
{
System.out.println("hu...hu...");
}
/**
* call breath()
*/
public void repeatBreath(int rep)
{
int i;
for(i = 0; i < rep; i++) {
this.breath();
}
}
private int height; // encapsulated, for internal use
}
Internal methods are not affected by encapsulation. Human's internal methods can call any member, even height and breath() set to private
External methods can only call public members. When we are outside Human, such as in Test, we can only call members specified as public in Human, but not members specified as private.
Through encapsulation, the Human class only retains the following methods as interfaces:
1.getHeight()
2.growHeight()
3.repBreath()
We can represent the Human class and its interface as follows:
"Remote control with case"
If we force call height from main:
Copy the code code as follows:
System.out.println(aPerson.height);
There will be the following error message:
Copy the code code as follows:
Test.java:6: height has private access in Human
System.out.println(aPerson.height);
^
1 error
Beep, you got an electric shock! A member declared as private cannot be called externally.
In Java's usual specification, data members that express state (such as height) are set to private. Modifications to data members must be performed through the methods provided by the interface (such as getHeight() and growHeight()). This specification plays a role in protecting data. Users cannot directly modify data and must use corresponding methods to read and write data. Class designers can add data usage specifications to interface methods.
class encapsulation
In a .java file, there is and can only be one class with the public keyword, such as the Test class above. So, from any other class, we can call this class directly. Human class has no keyword. Earlier, members of our objects did not have keywords. This lack of keywords also represents a kind of visibility, which I will go into in depth when explaining packages.
Practice encapsulating a Torch class to represent a flashlight. The interface has switching and charging. The internal members have power.
Summarize
encapsulation, interface
private, public