Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encapsulation vs Data Hiding - Java

Interviewer: What is encapsulation and how do you achieve it in Java?

Me: Encapsulation is a mechanism to hide information from the client. The information may be data or implementation or algorithm. We achieve this using access modifiers.

Interviewer: This is data hiding. How do we achieve encapsulation in Java?

Me: uummmm

Concrete Question: Other than 'Access Modifiers', what is the way to implement Encapsulation in Java?

like image 244
Sandeep Jindal Avatar asked Aug 17 '12 21:08

Sandeep Jindal


People also ask

What is the difference between encapsulation and data hiding in Java?

While data hiding focuses on restricting data use in a program to assure data security, data encapsulation focuses on wrapping (or encapsulating) the complex data to present a simpler view to the user. In data hiding, the data has to be defined as private only. In data encapsulation, the data can be public or private.

Is information hiding the same as encapsulation?

Encapsulation may be considered to be the same as information hiding, but the term is often used to describe the practical implementation of information hiding, especially in object-oriented programming.

What is the difference between encapsulation abstraction and data hiding?

Abstraction hides complexity by giving you a more abstract picture, a sort of 10,000 feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.

What is encapsulation and information hiding in Java?

By definition, encapsulation describes the idea of bundling data and methods that work on that data within one unit, like a class in Java. This concept is also often used to hide the internal representation, or state of an object from the outside. This is called information hiding.


2 Answers

More generally encapsulation refers simply to bundling the data (e.g. of an object) with the operations on that data. So you have a class encapsulating data - fields - along with the methods for manipulating that data.

But encapsulation is also sometimes used in the same way as your answer, and indeed, one of the points of bundling data and methods is to hide the implementation.

I suppose a better answer than just use methods and make all fields private is: use interfaces. This way, operations on an object are purely based on the interface contract, and are in no way tied to the fields or helper methods used to implement that contract internally.

like image 163
pb2q Avatar answered Sep 18 '22 12:09

pb2q


Encapsulation

In general Encapsulation means to bundle similar items, simple as that.

For example, take a Student class where we will have students instance variables and behaviors/methods acting on those instance variables @ one place.

Why it is important? We don't want our code scattered all around in our code Base.

If let say we need to make changes then we need find the variants(of that changes) at all the places. By Bundling similar items we are just avoiding such scenarios and it also helps to make our Bundled code more focused.

Data Hiding

It just provides a way to protect your data from the outside world. What it means is, lets say if I made my instance variable public, then anyone can change its state. But if we make our instance variable private/protected then actually we are restricting outside entities from making changes to it.

Comparison/Discussion

Now question arises, in what respect are we making our variables protected?

Again we need to understand that Encapsulation is just the container we need to place our similar items.

Its just act like a Black box to outside world. Outside world (I mean to say clients/consumers: using our Student class) don't know the internal details/implementation details of the Student class and actually they should not care about the internal details/implementation details of the class. They just want some methods/APIs so that they can use them in their client application.

So my point is all student related behaviors/variables are placed in a Black box which we are calling it as a class. Now its up to designer of the class what are the elements of the class should be hidden and what should not be hidden from outside world.

Now coming back to the question In Java: we are making our variables private it means they are class protected.If we want our instance variables to be accessible throughout the package then they are package protected. Through out project they are public. So what I mean to say is to hide data you need some sort of container where you will put your data and hide with respect to container.

So what I feel Data Hiding is not possible without Encapsulation. You can't hide your data without putting it in some form of container. Again I'm reminding you that I'm putting this on the Object Oriented Language context.

But yes Encapsulation is possible without hiding your data. Put all things public and you can the see the affect.

like image 32
bharatj Avatar answered Sep 20 '22 12:09

bharatj