Interviewer: What is encapsulation and how do you achieve it in Java?
Me: Encapsulation is a mechanism to hide
The easiest definition of encapsulation is "Binding data and code that operates data into a single unit", so that the data can't be accessed directly by outside world.
In structure language like c data is declared at the start of the block and used directly, accessed openly(Any tom, dick and harry can access data).
Data hiding is a concept to restrict data access directly and openly to any tom dick and harry.
Encapsulation is one of the way to achieve Data hiding.
You achieve data hiding by putting data and function together (Encapsulation).
How you achieve in JAVA, see any getter(), setter() function example in JAVA. Check this
How do getters and setters work?
Abstraction says that what needed to interact with object.
Encapsulation means encapsulate object properties , state and behaviours into single logical unit called class.
Data hiding says that hiding inner implementation of objects means private methods and properties, the object that use to maintain their state.
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.
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.
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.