-1

I am using getters and setters to for the purpose of encapsulation.

public class Student {

    private String studentID;
    private String studentName;
    private String address;

    public Student(){
        //default constructor
    }

    public Student(String studentID, String studentName, String address) {
        super();
        this.studentID = studentID;
        this.studentName = studentName;
        this.address = address;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

the variables studentID, studentName and address are declared as private, with the intention of encapsulation.

but we could also do the same task by making the variable accessibility level from private to public, is it really helps to apply encapsulation by the use of setters and getters?

only I can understand the use of getter and setter is users of the class dows not need to have an idea about the names of the variables used in the class as setters and getters makes sense to the users of the class

ex- objectofStudentClass.setStudentID("S0001");

Is there any difference between main difference between making getters and setters instead if making the variable access level to public.

Question 2: also here I have made parameterized constructor matching to the variables/fields in the class Student. is that throw away the concept of encapsulation?

2 Answers2

1

The first thing to note is that public getters and setters do not provide encapsulation:

[…] getters and setters do not achieve encapsulation or information hiding: they are a language-legitimized way to violate them.

James O. Coplien & Gertrud Bjørnvig. Lean Architecture. Wiley. 2010. p. 134.

So the answer to your question as to whether there's a difference between them and just having a public field is: there is no difference as far as encapsulation is concerned.

What they do provide though is support for the Open-closed principle. You can later change the behaviour of the getter and setter, to add a guard cause for example, without changing the API. This though leads down another potentially bad path, as any getter/setter that doesn't just get/set a field risks breaking the POLA principle.

You final question leads you to the "correct" way to use getters: use a constructor to set the fields and only provide getters. That provides true encapsulation and enables your fields to be invariant, which improves thread-safety.

David Arno
  • 39,599
  • 9
  • 94
  • 129
0

Yes, there is a huge difference.

Having getters and setters allows you to change their implementation (for instance, to add range checking, audit logging, statistics updates etc.) in the future without having to change all client code. Having a public variable would make such maintenance a breaking change. Very often this is the difference between a change being feasible and being too invasive/risky to perform.

Kilian Foth
  • 110,899