Encapsulation in Java

Encapsulation in Java

Encapsulation comes from the word ‘encapsulate’, which is primarily the technique by which certain objects or entities are wrapped under one single unit. It is the method of wrapping data under one heading or unit. It is the method of binding together code and the data the code will require in its execution. As data is encapsulated under a blanket, encapsulation can be thought of as a protective shield and prevents data from being accessed by objects and methods that are not allowed to do so.

  • If we are to go by the sheer technicality of this feature in Java, all the variables or data of a class are hidden from other classes. If one were to access this data, one could only do so via any member function of the class where the original data has been declared.
  • As in encapsulation, data hiding is the process of hiding data of one class from other classes. The way in which it is conducted is that the members are declared to be private. This reduces the scope of the members and methods only to the current class. Also, the end-user is only concerned with the user interface. He/she does not really have to know the nuances of the code. Therefore, encapsulation implements a mixture of data hiding and abstraction.
  • A simple way to achieve encapsulation in your code is to declare all the variables of your class to be private. This should be followed by making all the sets and get the methods of the class to be public.

Sample Code

The following code will demonstrate how you can implement encapsulation in your code.

class Demo
{
private String name;
private int roll;
private age;
public int getAge ()
{
return age;
}
public int getRoll()
{
return roll;
}
public String getName ()
{
return name;
}
public void setAge (int newAge)
{
age = newAge;
}
public void setName (String newName)
{
name = newName;
}
public void setRoll (int newRoll)
{
roll = newRoll;
}
}
public class DemoEnc
{
public static void main (String args [])
{
Demo obj = new Obj ();
obj.setName (“Agni”);
obj.setAge (20);
obj.setRoll (52)
System.out.println (“Name” + obj.getName());
System.out.println (“Age” + obj.getAge());
System.out.println (“Roll” + obj.getRoll());
}
}

 

 

OUTPUT
Name : Harsh
Age : 19
Roll : 51

The program above, from the source code, as is evident, uses the encapsulation feature by setting all the member methods to be private. It also sets all the get methods like getAge(), getName(), getRoll() to public. The set methods like setName(, setAge(), and setRoll() are also declared public and are used to update or modify the values of the variables.

Advantages of Encapsulation

  • Data Hiding – As mentioned before, encapsulation is hiding data by grouping the data. Data hiding is the perfect match for this situation. It is because data hiding only furnishes as much as the user wants without exposing the important details.
  • Increased Flexibility – Variables in the class can either be read-only or write-know as per the situation. If the variables are made read-only, this will mean that users will have to stop using the setter and getter methods for the variable of the class.
  • Re-usability: Encapsulation improves the re-usability of the codes, and it is very easy to change as per new requirements.
  • Testing code is easy – The code can be tested by deploying unit testing.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.