I have a confusion in how do getter and setter provide encapsulation. I mean what is the difference between assigning value to a variable directly and assigning them through getter and setter. Also lets say we have two class A & B. While using getter and setter to set values from class B we have to make an object of class A in class B, so how are the variables encapsulated when we already know in which class they are defined.
3 Answers
You encapsulate the variables declaring them private
so other classes can't access them without a getter or setter method. For example:
private int num; //variable to-be defined from constructor
public int getNum(){
return num;
}
Other objects can't access the private num
variable from this object without using the getNum()
method.
If you want to change the encapsulated variable:
public void changeNum(int n){
num = n;
}
The num
variable is changed without breaking the java syntax.

- 1
- 1
With setters we can apply extra validation on the specified value which won't be possible when you assign the value directly to the property. A example could be to check whether the value is not null for example:
public void setVariable(Object value) {
if (value == null) {
throw new IllegalArgumentException();
} else {
...
}
}
Other possibilities you get with setters is that you can apply extra computations. If you would for example have a BankAccount instance and you change the interest the setter could initiate recalculation of the interest that has to be paid to the user.
A possibility you get by applying a getter is that you can return default value in case the value is not set yet.
Another appliance of the getter and setters is restricting the access to the variable. We could for example make the getter public while the setter is protected. This means that everybody can read the property but only classes that extend the other class, or are in the same package can set the value.
Regarding your second question. I don't fully understand the question. Could you elaborate a bit more on that one.

- 938
- 6
- 5
-
Thank you for your answer. As asked, my second question is that setter and getter is used for encapsulation as we already know. But if we are to achieve this encapsulation we have to hide all the necessary details. Lets say we want to change the value of a variable defined in a class A from class B. So class B should have nothing except setter and getter so that nobody knows in which class or which variable the value is set. But the way it's done is we have to create an oject of class A in B by which its already known that the variable is defined in which class. – Pratik Paul May 07 '16 at 06:14
-
So my question is when we already know in which class the variables are defined and we can directly change the values from there, why use a much longer process of setting the value using methods instead of assigning the values by using object.value – Pratik Paul May 07 '16 at 06:16
-
One of the reasons to do this is about control. There is probably a reason why the values of class A are set via B. It could for example be that class B has to do some validation/computation after the value of class A has changed. If anybody can change the value of A, class B will never know about the change in A and computation will not be performed. – uniknow May 07 '16 at 06:23
-
Okay. thanks again for your answer. But still there is a bit of confusion which I hope you could clear up. From data hiding perspective,why not change the value of variable directly. Why use getter and setter. I mean ultimately the main goal is to change the value of the variable. So why such long process. Can you give me a real life example so that I can have a clear concept. – Pratik Paul May 07 '16 at 06:42
-
It all depends from the appliance of the variable. If changing the variable requires checks and/or side effect (like computations based on value) getters and setters are a good approach. A real life example for setters are for example null checks. – uniknow May 07 '16 at 07:02
Getters are public and setters are private (usually). This means that any objects can read our class's variables, but only our class's objects can change their value. This provides encapsulation in the sense "it doesn't give other objects more access than they need". This is logical because say our class is a car and our object is Ferrari. The new variable is Horsepower of the Ferrari, say. Only Ferrari makers can change the value of the horsepower that Ferrari gives. Others can only read, and not change.
Answering your second question, when an object of A is made, you will need to initialize all (or at least some) variables of A. This is analogous to, in the above analogy, creating a new Ferrari car. Once you create the Ferrari, you will obviously have to know all of its variables. Also, here class B is assumed to be a " safe" class, say Main. B cannot be Mercedes or Audi. (This is something the programmer needs to take care of, again by using public/private encapsulation to the Ferrari class) Mercedes or Audi classes will not have the permissions to create a Ferrari car. Only "Main" and "Ferrari" will have that.

- 466
- 4
- 11