Easy Tutorial
❮ Number Sin Java8 New Features ❯

Java Encapsulation


In object-oriented programming, encapsulation (English: Encapsulation) refers to a method of wrapping and hiding the implementation details of abstract function interfaces.

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

To access the code and data of the class, strict interface control is required.

The primary function of encapsulation is that we can modify our implementation code without having to modify the program segments that call our code.

Proper encapsulation can make the code easier to understand and maintain, and also enhance the security of the code.

Advantages of Encapsulation

-

  1. Good encapsulation can reduce coupling.

-

  1. The internal structure of the class can be freely modified.

-

  1. Allows for more precise control over member variables.

-

  1. Hides information, implements details.

Steps to Implement Java Encapsulation

  1. Modify the visibility of the attributes to restrict access to them (usually restricted 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, meaning they can only be accessed by the class itself, and are inaccessible to other classes, thus hiding the information.

  1. Provide public methods for accessing each value attribute, that is, create a pair of getter and setter methods for accessing private attributes, for example:
    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 between instance variables (private String name) and local variables (setName(String name) where the name variable is local).


Example

Let's look at an example of a Java encapsulated 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;
   }
}

In the above example, public methods are the entry points for external classes to access the class's member variables.

Typically, these methods are referred to as getter and setter methods.

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

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

RunEncap.java File Code:

/* 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 with the following result:

Name : James Age : 20
❮ Number Sin Java8 New Features ❯