< Prev
Next >

Encapsulation





The key advantage of using an Object Oriented Programming language like Java is that it provides your code - security, flexibility and its easy maintainability through encapsulation.




Why encapsulation?



Let's understand the need of encapsulation, by first seeing a class that is an example of a bad encapsulation and the drawbacks associated with it.




An example of a bad encapsulation.


We are creating a class with a public instance variable, that should never be set to a negative value.

class Stats
{
public int  weight; //weight with public access, can be accessed by anyone 

public static void main(String... ar)
{
Stats ob = new Stats();
ob.weight = -100; //weight set to a negative value, legal but wrong.
}

}

In this code, we have made a class Stats with its public instance variables, weight. In the main method, we have created an object of this class and through it we have directly accessed its public instance variable to set it to a negative value i.e. ob.weight=-100, which will cause a problem as weight is never a negative value.




Drawbacks of a bad encapsulation.


Giving a public access to instance variable of your class makes it unsafe from an unauthorized access, hence, anyone can directly access it directly through class's object and set its value even beyond its permissible range, which may cause an issue.

Now in order to rectify this issue, if you modify the access modifier of instance variable, weight, to private, you will end up breaking the code of everyone who are already inheriting your Stats class and are directly accessing its instance variable, weight. Thus such code is not only less secure but also less flexible to change.

Hence, you should only create your classes by following proper encapsulation. Let's see how encapsulation comes to the rescue.









An example of good encapsulation


class Stats
{
private int  weight;



public getWeight()  //Getter method to get weight.
{
return weight;
}



public void setWeight(int wt)  //Setter method to set weight.
{
if(w<0)
	System.out.println("Please enter weight greater than zero");

else
	weight=w;
}


public static void main(String... ar)
{
Stats ob= new Stats();
ob.setWeight(100);
}

}

In this code, we have restricted the access to weight instance variable by declaring it private, hence, anyone wishing to set the value of weight instance variable, will have to call setWeight() method. In setWeight() method we have an if-else condition that makes sure that instance variable, weight's value should not be set to a negative value.

Following such proper encapsulation also provides flexibility to your code, allowing you to make changes to you code, without breaking the code of others who were accessing your code, for example- tomorrow, you may decide to put in a different if-else condition, like - weight should be set between 50 and 100 kg/pounds. Modifying your code in such way will not affect anyone accessing your class.




Benefits of encapsulation


Through such proper Encapsulation, now your code is more flexible to the change, easy to maintain and you've also secured and protected your data(instance variables) from the direct access by giving it a private access.




Difference between encapsulation and abstraction


While encapsulation helps in hiding the data(instance variables) of a class from an illegal access, abstraction provides essential general features without providing implementation details, by using an abstract class or an interface.


Example of Abstraction

Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2