Encapsulation Vs Plain

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

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

But can I take advantage of this without properly using encapsulation? because each field of an object is different from each other.

// 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());
  }
}
+4
source share
4 answers

Your question is quite interesting. I will try to answer you in detail.

, . private, . .

, .. getter setter, . , , .

, getter setter. , . , getter.

.

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());
    } 
}

1) . getEmpName() setEmpName(), - .

2) ( ) ( ).

3) , . , , , emters getter.

0
  • , . , ..
  • , . ( ) . , - - , .
  • . , , .


  • , . ( )
  • , . , .
+3

- private .

public class Person {

private String name;
private Date dob;
private transient Integer age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Date getDob() {
    return dob;
}

public void setDob(Date dob) {
    this.dob = dob;
}

public int getAge() {
    if (age == null) {
        Calendar dob_cal = Calendar.getInstance();
        dob_cal.setTime(dob);
        Calendar today = Calendar.getInstance();
        age = today.get(Calendar.YEAR) - dob_cal.get(Calendar.YEAR);
        if (today.get(Calendar.MONTH) < dob_cal.get(Calendar.MONTH)) {
            age--;
        } else if (today.get(Calendar.MONTH) == dob_cal.get(Calendar.MONTH)
                && today.get(Calendar.DAY_OF_MONTH) < dob_cal.get(Calendar.DAY_OF_MONTH)) {
            age--;
        }
    }
    return age;
  }
}

getAge() , , , .

0

- . , , . , , , . - - getVolume getSurfaceArea. , , , . , , ( ), . , ( Java). .

It should be noted that there are times when setters / getters are not used and variables are affected. Even in Java, it sometimes makes sense to not hide instance variables.

0
source

Source: https://habr.com/ru/post/1598800/


All Articles