Search code examples
javaencapsulation

Encapsulation Vs Plain


Why should I use encapsulation if the code below will produce the same result?

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.

But I can use this benefit whithout using encapsulation right? because every object's field differs from each other's field.

// Person.java
public class Person {
  // Plain
  public String name;

  // Uses encapsulation
  private String name2;
  public void setName(String name2) {
    this.name2 = name2;
  }
  public String getName() {
    return name2;
  }
}

// Main.java
public class Main() {
  public static void main(String[] args) {
    // Plain
    Person person = new Person();
    person.name = "Jordan";
    System.out.println(person.name);

    // Uses encapsulation
    Person person2=new Person();
    person2.setName("Jordan");
    System.out.println(person2.getName());
  }
}

Solution

  • Your question is quite interesting. I will try to answer it to you in-depth.

    The main idea behind encapsulation is to hide the data and its implementation details from other users. If we make a data member private then it can only be accessed within the same class. No other class can ever access that piece of data directly.

    But we can define an interface, i.e. public getter and setter methods to update the data from other classes. This ensures that the private data remains inaccessible to others and can only be accessed by the public methods you provide.

    For instance, you may decide to provide only the getter method for a particular data member and no setter method. This ensures that no other class can change or update your data member in any possible way. They can only get the value if they want using the getter method.

    This is why encapsulation is also known as Data Hiding.

    Example

    public class EncapsulationDemo{
        private int ssn;
        private String empName;
        private int empAge;
    
        //Getter and Setter methods
        public int getEmpSSN(){
            return ssn;
        }
    
        public String getEmpName(){
            return empName;
        }
    
        public int getEmpAge(){
            return empAge;
        }
    
        public void setEmpAge(int newValue){
            empAge = newValue;
        }
    
        public void setEmpName(String newValue){
            empName = newValue;
        }
    
        public void setEmpSSN(int newValue){
            ssn = newValue;
        }
    }
    public class EncapsTest{
        public static void main(String args[]){
             EncapsulationDemo obj = new EncapsulationDemo();
             obj.setEmpName("Mario");
             obj.setEmpAge(32);
             obj.setEmpSSN(112233);
             System.out.println("Employee Name: " + obj.getEmpName());
             System.out.println("Employee SSN: " + obj.getEmpSSN());
             System.out.println("Employee Age: " + obj.getEmpAge());
        } 
    }
    

    Advantages

    1) It provides flexibility to the code and makes it easily maintainable. We can change the implementation of getEmpName() or setEmpName() without affecting any other outside code.

    2) We can make data members read-only (by only defining getters) or write-only (by only defining setters) anytime.

    3) Other users will not be knowing what is going on behind-the-scenes. They will only know that to update a data, we need to call the settter methods and to get a data we need to call the getter emthods.