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
Continue reading JAVA – Data Hiding and Encapsulation