What Is Encapsulation In Java?

3

3 Answers

Anonymous Profile
Anonymous answered
If you have been sick, you would have had medicines for sure. One of the tablets that you would have received is a capsule. I hope you would have seen it.

Now this kind of tablet, is a bit different from the others. From the outside its just a cap, and it hides everything that is contained within. But what lies inside may be 2 or 3 or more powders loosely arranged and packed within.

An object is something similar. It is created with the immense power of a class. While the composition of the class could be anything (as compared to the capsule), one may not know what is contained when you create the object handle

as in
A obj = new A();

you may say obj here is like the capsule to all those who want to consume it in their programs.

So, with this object one can use its inherent power.

Thus this concept of hiding away its true power is known as Encapsulation.

Hope this helps.
Gopinath Sundaram Profile
Encapsulation is hiding of implementation details.Let me tell you an example for your second question : Consider a house without walls,Is it safe - No,it isn't.For safety and protection,we built walls on both sides(encapsulation is there).But,now the house is surrounded by only walls,what is the use?? So,you keep a door,thus making the house usable(Abstraction+Encapsulation).Abstraction is extracting essential properties.
Anonymous Profile
Anonymous answered
Hide the data and code from outside interface and misuse.

Answer Question

Anonymous