JAVA – Data Hiding and Encapsulation

Data hiding or encapsulation is a technique where data is hidden within a class. Only methods that call this data have access to it. Data encapsulation hides private methods and other data members inside the class, which can be accessed by the public methods.

This method hides the code which is written to perform the operation for which the class is written. This will help the programmers to easily modify the code of the class without worrying about the existing code details.

Encapsulation of data also restricts the programmer access to the entire program code. Often, some classes within a program contain a number of fields which are dependant on each other and should not be changed. Also, they should not be accessed by everyone. A programmer can change any of these fields without changing others, and this can result as a class in inconsistent state.

They can instead use a method to change these fields, which will make sure that everything is done to keep class in working condition and consistent. Similarly, we can define internal methods of a class and can prevent calling them from outside the class by other user. The programs written with data encapsulation in mind, describes only those methods that define allowed operations by objects of that particular class. This class has been carefully tested and will work as desired. However, if a program is written without considering all this, it is possible that you need to put a lot of testing effort.

Sometime fields and methods make the programming code look cluttered and unclear. If the number of fields is less, not only the code looks clear but also it becomes easier understand the code. Also, it leads to minimum documentation, saving both programming time and effort.

Java supports data encapsulation and hiding by the Access Control mechanisms. According to this method, all of the methods and fields of any class, can be used and defined only inside the body of the class.

Java also has some rules that define how we can use the members of the class, from outside the class. The Java language uses certain access modifiers for this purpose, such as the keywords public, private and protected. These keywords specify the access rules for a certain method or field.

You can access a package from the code defined inside the package. This code can be accessed by other packages too, and access rules depends on how the packages are deployed. For example, a class my_files can store all the data within it in a directory. The user can then ask for the Read access of the my_files class along with the underlying directory.

The java language does not support package control. This kind of control is done on all levels of classes and members of classes.

Another type of data encapsulation supported by Java is the Access to members. The members of the class can be accessed within the body of the class itself. Not only this, the members can also be accessed throughout the package in which the class is defined, by default.

Continue reading JAVA – Data Hiding and Encapsulation