Encapsulation vs Data Hiding - Java

encapsulation java
difference between encapsulation and data hiding in c#
data hiding in java javatpoint
encapsulation vs abstraction
abstraction in java
encapsulation vs information hiding
abstraction vs data hiding
data encapsulation

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?


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.

Encapsulation vs Data Hiding - Java, 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  Data hiding can be achieved through the encapsulation, as encapsulation is a subprocess of data hiding. Definition of Encapsulation Encapsulation is binding the code and data together in a capsule to hide the complexity of a class.


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.

OOP Concept for Beginners: What is Encapsulation, Encapsulation is one of the fundamental concepts in OOP. It describes the idea of bundling data and methods that work on that It describes the idea of bundling data and methods that work on that data within one unit, e.g., a class in Java. This concept is also often used to hide the internal representation,  Encapsulation refers to the bundling of data with the methods that operate on that data. Often that definition is misconstrued to mean that the data is somehow hidden. In Java, you can have encapsulated data that is not hidden at all. However, hiding data is not the full extent of information hiding.


Encapsulation: Take the example of a capsule. If you open it, it contains a lot of ingredients in it. Object Oriented programming's Encapsulation is also like that. As the name suggests "Encapsulation means to encapsulate (make a capsule of) all the data members, attributes and corresponding methods within one single capsule.

How do you do it: Lets say you made a class named "Car". Now car has a color price and model. These are attributes, and it has a run method. So here you've encapsulated all these attributes and methods of a vehicle named "Car". When you create an instance of car like so

Car myCar = new Car();

You can access all the attributes of Car using the myCar variable.

"Data hiding": Data hiding is controlled in Java using access modifiers. To access the data members you use ACCESSORS while to modify the data you use "Mutators". Java doesn't provide accessors and mutators by itself, you create them yourself (getters and setters). While C# provides PROPERTIES to do so.

What is the difference between "data hiding" and "encapsulation , I'm reading "Java concurrency in practice" and there is said: "Fortunately, the same object-oriented techniques that help you write well-organized, maintainable  Encapsulation is most often achieved through information hiding (not just data hiding), which is the process of hiding all the secrets of an object that do not contribute to its essential characteristics; typically, the structure of an object is hidden, as well as the implementation of its methods.


I hate to do this but, from Wikipedia:

In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  1. A language mechanism for restricting 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

Your explanation was more along the lines of the first notion, the interviewer was looking for something on the second.

Java - Encapsulation, Encapsulation in Java is a mechanism of wrapping the data (variables) and code In encapsulation, the variables of a class will be hidden from other classes,  Summary – Data Hiding vs Encapsulation. Data Hiding and Encapsulation are two concepts of OOP. Data hiding is the process of protecting the members of the class from unauthorized access. Encapsulation is the process of wrapping the data members and methods into a single unit. That is the difference between data hiding and encapsulation.


Data encapsulation refers to the mechanism of keeping all the related properties and method into a single entity.

Example: A Car. It keeps steering wheels, tires, engine and all the related stuffs into one collective entity known as Car.

In programming languages the encapsulation is achieved with classes. A class contains all the properties and related methods in one single entity that is designed to do a specific task.


Data hiding refers to hiding the unimportant details from the user and show him only the relevant data.

Example: When we press brakes or accelerator in a car, we don't know what's happening behind the scene (how its increases its speed or how its applying the brakes into tires). All we know is that we have brake and accelerator to perform the desired outcome.

In programming this is done with the help of access modifiers. Private members are not accessible from outside the class and only public members are accessible to users. The private members can only be accessed from members of the class thus providing the security for private members to be directly evaluated from the outside of the class.

Encapsulation in Java, Technically in encapsulation, the variables or data of a class is hidden from any the abstraction concept, so it is also known as combination of data-hiding and  Data encapsulation is a process, while data hiding is both a process and a technique. They both share the category of object-oriented programming. The data in data encapsulation is either public or private, while the data in data hiding is private and non-accessible. Data encapsulation is one of the mechanisms of data hiding.


Data Hiding and Encapsulation (Java in a Nutshell), Data Hiding and Encapsulation. We started this chapter by describing a class as "​a collection of data and methods." One of the important object-oriented  Encapsulation puts the data safe from the outside world by binding the data and codes into single unit. Abstraction lets you focus on what the object does instead of how it does, while Encapsulation means hiding the internal details of how an object works.


Difference Between Data Hiding and Data Encapsulation , Summary. Data encapsulation is a process, while data hiding is both a process and a technique. They both share the category of object-oriented  Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.


OOAD Part2: Encapsulation and Data Hiding Vs Abstraction , Second, encapsulation is a technique for protecting data from misuse by the outside world, which is referred as 'information hiding' or 'data hiding  The isolation of the data from the direct access by the program is called data hiding or information hiding. For implementing data hiding, encapsulation is used where data and function of a class is protected from unauthorized access. In contrast, when the data and function is encased into a single unit is known as encapsulation.