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?
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
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.
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.
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:
- A language mechanism for restricting access to some of the object's components.
- 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.
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.