Data Hiding and Abstraction in Java : Free Java Tutorials

Data Hiding and Abstraction are two of the most important features of Object Oriented Programming in Java. These two features are the main reason behind the security and maintainability that Java brings. We will understand in detail about these two features in this tutorial.

Data Hiding in Java

The internal data of any class should not be accessed from outside area of that class. This Object-Oriented Programming concept is Data Hiding.

We can only access internal data after proper validation. For example, we can access our Gmail account only after giving our proper validation via our login credentials.

There are mainly three access modifiers in Java: private, protected and public.

By declaring data variable as private, we can achieve data hiding. The main advantage of data hiding is security.

Therefore, it is highly recommended to declare data variable as private. (In C language, by default, variables are private only. We cannot access a variable from outside the function unless we declared it globally.)


Abstraction in Java

Hiding internal implementation and just highlight set of features that we are offering is the concept of Abstraction.

For example, through Paytm app UI, Paytm is highlighting the set of services/features that they are offering like add money, pay bills, book tickets, send money etc. However, they do not expose their internal implementation of these features. This is the best example of abstraction.

Abstraction has following advantages:

  1. Security – since the end user is not able to access internal implementation, we can say that our internal data and business logic is secure from any outside threats.
  2. Ease of Enhancements – since abstraction does not allow the end user to access internal logic, we can easily modify or replace without affecting the end user. Hence, the enhancement will become very easy.
  3. Improvement of Maintainability and Easiness – abstraction improves maintainability and easiness of the overall system.

By using Interfaces and Abstract Classes, we can implement abstraction. We will see in detail about interfaces and abstract classes in upcoming tutorials.

This is all about Data Hiding and Abstraction in Java. Hope you find this tutorial helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *