Defines to keeping all the related members together in an object. Specifying member variables as private may hide the methods and variables. Objects should hide their inner workings from the outside part. Good encapsulation gives code modularity by preventing objects interacting with each other in an unexpected way, which in turn builds future development and refactoring efforts easy.
Being able to encapsulate members of a class is important for integrity and security. We may protect variables from unacceptable values. The sample code above describes how encapsulation may be used to protect the MyMarks object from having negative values. Any modification to member variable "vmarks" can only be laid out through the setter method setMarks(int mark). This prevents the object "MyMarks" from needing any negative values by throwing an exception.