
purpose of encapsulation?

The basic purpose of encapsulation is SECURITY. You won't want anyone to steal into your pages or your programs easily by using java. When you declare an instance variable(I hope you know that - the thing that an object knows about itself), probably you'd declare it somewhat like this [type][name]; However, this kind of declaration makes it easy for others to change what you have programmed.
If you encapsulate your instance variables, or simply add private keyword before it, the instance variable becomes impossible to be accessed normally. But, you can always access those by your Getter and Setter methods which return the variable to you and allow you to set it accordingly, but block changes by others.
I hope you've understood what I meant.
Thanks,
Ashish Gaurav
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.