Data Abstraction & Data Encapsulation in Java

What is Data Abstraction in Java?

Short Explanation

Data abstraction on the other hand is something completely different. It is used in OOP to unify all generic and most common attributes about a data structure to build a foundation and prevent redundancy in your software design. That’s basically where inheritance comes into the picture.

Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the “abstract form of anything”. We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.[1]

TL;DR: Abstraction is hiding the implementation details.

Long Explanation

Abstraction is most used while dealing with data structures. It is hard to see it in small scale programs, or at least that’s my opinion.

When programming in the real world, we often create data structures that other programmers will use. If I create a class to do something, I want another programmer to be able to take that class and use it for himself. We often call that programmer the implementer.

Say that you built a robot that runs on Java. There are various methods that can be used on it. walkLeft(), walkRight(), walkFoward(), etc. When you call one of these methods, do you know what the implementation is doing? No, you don’t. We can assume that the implementation would be extremely complicated. You’re the engineer, so leave the software to the programmers. You built the robot, you shouldn’t need to know how the software works. If an engineer had to understand how the methods actually worked, then they would have to be programmers as well. All that matters is that when you call the method it does what is promised.

This is one reason for interfaces(although there are many more).

Lets create an interface that moves the robot:

The comments that we write above each method make it very clear what needs to be true for the method to run and what happens after the method is called. We call this the contract of a method. Notice that the implementer doesn’t need to know a thing about how the robot actually moves left or right. All that matters is that if the preconditions are met, then the post conditions will be met. It is guaranteed to work. So now you can build any type of robot: dog, cat, person, etc. If you implement this interface, then these 4 methods (and many more) will work on your robot.

Examples of abstraction? You use them almost every time you program. Think about common data structures: Array list, map, linked list, etc. When you have an array list called list1 and you call list1.add(person1), do you have any idea of how it does it? No, because you don’t need to. Someone wrote that class for you and figured out how an object would be added to an array list. Now you don’t need to worry about it. As long as you satisfy the preconditions list for the method that are (hopefully) written in the class documentation, then at least one post condition will be satisfied : person1 will be added to list1. The fact that when you work with these data structures you don’t need to know the implementation of the methods to actually use them is exactly what the definition in your book is talking about.[3]

What is Data Encapsulation in Java?

Encapsulation is a concept in OOP used to determine the degree of safety and type of interaction of your data types within a class with the outside world (the rest of your application / framework). For instance having public getter and setter for a variable, although this is the most trivial example and it can get much more complex.

Encapsulate means to hide. Encapsulation is also called data hiding. You can think Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation.[1]

Information Hiding is hiding the data which is being affected by that implementation. Use of private and public comes under this. For example, hiding the variables of the classes.

TL;DR: Encapsulation is just grouping all similar data and functions into a group; For example, Class in programming.

Long Explanation

What is encapsulation? Well, encapsulation can be defined as:

  1. A language mechanism for restricting direct access to some of the object’s components.
  2. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

An example of the first definition is what you mentioned: using modifiers. However, public is not included in that. By using public, we can access variables and methods from almost anywhere. We don’t want that. Also, most of the time it is better to just use private modifiers when striving for encapsulation (when we are working with objects at least). Although there are some reasons to use protected. By using private, we are limiting the visibility of methods and fields. As you know, to then view or edit these private variables, we use getters and setters (and make these methods public). You can also read the first paragraph of this link to see it summed up a little bit. Notice that they talk about some abstraction in their explanation.

More definitions of Encapsulation:

  1. A concept which enforces protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of a program due to change in protected code
  2. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them.

In the example that I gave of the robot, that is exactly what happened. We hid a lot of the information from the user. In this way, we also achieve abstraction. The same goes for Array List and the other containers.

Encapsulation is an important concept, and like the other OOP concepts, it takes a little bit to understand. I still come across new ways in which Encapsulation is used, or often someone such as a professor points out encapsulation to me in a scenario I have been working with for a long time, but I just didn’t notice that it was at work.

What is the Difference Between Abstraction & Encapsulation in Java?

First, Abstraction is implemented using interface and abstract class, while Encapsulation is implemented using private and protected access modifier.

Personally, I understand that abstraction is to make thing simpler; that’s why you’d use interfaces and abstract classes in data abstraction. And I understand that encapsulation is to hide information or hide access from the user, therefore the use of protected access modifiers.

Second, OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.[1]

Here’s another way to explain the difference between Abstraction & Encapsulation[2]:

Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object… encapsulation focuses upon the implementation that gives rise to this behavior… encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics.

TL;DR: You do abstraction when deciding what to implement. You do encapsulation when hiding something that you have implemented.

Links

Paradigm of Class Abstraction & Encapsulation
Sources

  1. Difference between Encapsulation and Abstraction in OOPS – [Source]
  2. Abstraction VS Information Hiding VS Encapsulation – [Source]
  3. [Reddit]

What's Your Opinion?