encapsulation

In object-oriented programming, Encapsulation is a method of wrapping and hiding the implementation details of abstract functional interfaces.

Encapsulation can be thought of as a protective barrier that prevents the code and data of that class from being randomly accessed by code defined by an external class.

Access to the code and data of this class must be controlled through strict interfaces.

The main feature of encapsulation is that we can modify our own implementation code without having to modify the snippet that calls our code.

Proper encapsulation makes your code easier to understand and maintain, and it also makes your code more secure.

Advantages of encapsulation

  • Good encapsulation reduces coupling.
  • The structure inside a class can be freely modified.
  • You can have more precise control over member variables.
  • Hide information, implement details.

Steps to implement Java encapsulation

  1. Modify the visibility of a property to restrict access to the property (generally limited to private), for example:

    public class Person {
        private String name;
        private int age;
    }
    Copy the code

    This code hides the information by making the name and age properties private and accessible only to this class and no other class.

  2. To provide external public method access to each value attribute, create a pair of assignment methods that are used to access private attributes, for example:

    public class Person{
        private String name;
        private int age;
    
        public int getAge(a){
          return age;
        }
    
        public String getName(a){
          return name;
        }
    
        public void setAge(int age){
          this.age = age;
        }
    
        public void setName(String name){
          this.name = name; }}Copy the code

    The this keyword is used to resolve conflicts of the same name between the instance variable (private String name) and the local variable (the name variable in setName(String name)).

The instance

public class EncapTest{
 
   private String name;
   private String idNum;
   private int age;
 
   public int getAge(a){
      return age;
   }
 
   public String getName(a){
      return name;
   }
 
   public String getIdNum(a){
      return idNum;
   }
 
   public void setAge( int newAge){
      age = newAge;
   }
 
   public void setName(String newName){
      name = newName;
   }
 
   public void setIdNum( String newId){ idNum = newId; }}Copy the code

In the above instance, the public method is the entry point for the external class to access the class member variables.

Normally, these methods are called getter and setter methods.

Therefore, any class that wants to access the private member variables in the class must do so through these getter and setter methods.

The following example shows how variables of the EncapTest class can be accessed:

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()); }}Copy the code
Name : James Age : 20
Copy the code