In object-oriented programming, Encapsulation refers to a way to wrap and hide the implementation details of an abstract function interface.
Encapsulation can be thought of as a protective barrier that prevents code and data of that class from being randomly accessed by code defined by external classes.
To access this type of code and data, it must pass strict interface control.
The main function of the package is that we can modify our own implementation code without modifying the program fragments that call our code.
Appropriate encapsulation can make the code easier to understand and maintain, and it also enhances the security of the code.
The advantages of the package
- 1. Good packaging reduces coupling.
- 2. The internal structure of the class can be freely modified.
- 3. More precise control of member variables can be made.
- 4. Hide information, implement details.
Steps to Implement Java Encapsulation
1. Modify the visibility of the property to restrict access to the property (generally limited to private), for example:
public class Person {
private String name;
private int age;
}
In this code, the name and age attributes are set to private, only this class can access, other classes can not access, so the information is hidden.
2. Provide external public method access to each value attribute, that is, create a pair of valued methods for accessing private attributes, such as:
public class Person{
private String name;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
}
The this keyword is used to resolve the conflict of the same name that occurs between the private string name and the name variable in the local variable (setName(String name)).
Examples
Let's look at an example of a java wrapper class:
EncapTest.java file code:
/* File name: EncapTest.java */
public class EncapTest{
private String name;
private String idNum;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getIdNum(){
return idNum;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setIdNum( String newId){
idNum = newId;
}
}
The public method in the above example is the entrance of the external class to access the class member variable.
In general, these methods are called getter and setter methods.
Therefore, any class that wants to access private member variables in a class must pass these getter and setter methods.
The following example illustrates how the variables of the EncapTest class are accessed:
RunEncap.java file code:
/* F file name: RunEncap.java */
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName()+
" Age : "+ encap.getAge());
}
}
The above code compiles and runs as follows:
Name : James Age : 20
Comments
Post a Comment