Advertisement



< Prev
Next >

Java - 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 for encapsulation, by first seeing a class that is an example of a bad encapsulation and the drawbacks associated with it.




An example of bad encapsulation.


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

//Java - Example of bad encapsulation

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.
}

}

Within the main method, we have created an object of our class Stat and through it, we have directly accessed its public instance variable, weight, 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 bad encapsulation.


Looking back at just our coding example, giving a public access to an instance variable of your class makes it vulnerable for 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 later on.

To rectify this issue in our last coding example, if you modify the access modifier of the instance variable, weight, to private, you will end up breaking the code of everyone who has already inherited your class, Stat, and is 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 the rules of a proper encapsulation. Let's see how encapsulation comes to the rescue.







Advertisement




An example of good encapsulation


//Java - Example of good encapsulation

class Stats
{
private int  weight;	       //A private instance variable, weight



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



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

else
	weight=wt;
}


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

}

Output:

Please enter weight greater than zero


In this code, we have restricted the access to our instance variable, weight, by declaring it private, hence, anyone wishing to set its value will have to call our public setter method, setWeight(). Within the setWeight() method, we have an if-else condition that makes sure that our instance variable, weight's value should not be set to a negative value, as you can see in the output.

Following such proper encapsulation also provides flexibility to your code, allowing you to make changes to your code, without breaking the code of others who were already 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 a way will not affect anyone accessing your class.




Benefits of encapsulation


Through such proper Encapsulation, your code is more flexible to changes, easy to maintain and you've also secured and protected your data(instance variables) from a direct unauthorized 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 i.e. by using an abstract class or an interface.

Let us show you an example of abstraction, where we are going to create an interface by providing only its essential general features i.e. without providing any implementation details.


Example of Abstraction:

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement