Definition: Encapsulation is the ability to package data, related behavior in an
object bundle and control/restrict access to them (both data and
function) from other objects. It is all about packaging related stuff
together and hide them from external elements.
Details: Imagine you made your class with the public instance variable and there are other programmers who are using your class and setting the instance variable directly as the below code demonstrate.
public class BadOO{
public int size;
public int weight;
......//other stuffs;
.......//setters and getter
}
//Some one using the class as below
public class ExploitBadOO{
public static void main(String args...){
BadOO = b = new BadOO();
b.size = -5; //Legal but bad
}
}
Now you are in trouble.How?
Suppose you wanted only positive value to be assigned to the the size .And you have setter method for that which does the validation.like below.
public void setSize(int value){
if(value<0){
//throw error value can not be negative
}else{
//do your work.
this.size = value;
}
}
That is not possible now because people can directly set the value of
size as you made that public they don't need to call your setter method
and set the value of size.
How are you going to control what value can be assigned to size?
You need to make one change in your class. You need to change the access modifier of your instance variable to restrict the access directly. You do this by changing access to private.
public class GoodOO{
private int size;
private int weight;
......//other stuffs;
.......//setters and getter
}
Now once you change the access modifier you break people's code who were using direct way to access you instance variable.
Now what you do?
Don't worry you have OO(Object Oriented) concept that is called encapsulation that comes to your rescue. If you would had followed the correct way of designing you class you would have not faced this issue.
So how you do achieve Encapsulation?
Encapsulation is achieved by following the below steps:
1. Keep restricted access to instance variable (with an access modifier, ofter private)
2. Make public getter and setter method to access your instance variable.
And suppose you want to change the implementation of setter method to something else?
Its easy now you just go ahead and change
the implementation and this will not break any ones code as they are
using setters and getter.
public void setSize(int value){
if(value<0){
//throw error value can not be negative
}else{
//do your work.
this.size = 5*value; //setting value as five time the original.
}
}
This ability to make change in your code without breaking the code of others who use your class is key benefit of Encapsulation.
Basically what you have done is you have hidden the implementation details behind the public programing interface.
So your final class will look like below
public class GoodOO{
private int size;
private int weight;
public void setSize(int value){
if(value<0){
//throw error value can not be negative
}else{
//do your work.
this.size = 5*value; //setting value as five time the original.
}
}
public int getSize(){
return this.size;
}
//getter and setter for weight.
}
PS: If you want to add more point please drop me a mail or write in comments below i will update the article.

No comments:
Post a Comment