group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
270 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
24 Jan 2019

What really is Encapsulation in Java



What really is Encapsulation in Java 


Probably encapsulation is the way we bind  data and methods acting on that data in a single unit..may be in a capsule , for sake of our imagination  .  This capsule  gives protection to the things inside it (code+data)  , although that depends upon out discretion ..we may decide to make the capsule transparent and visible and accessible to other guys as well  ,our Choice . So , we can achieve data hiding and thus data protection  by making our data private and providing getters abd setters , they are also known Accessor and Mutator respectively.


In general encapsulation allows you to  define Areas which shouldnt be modified by other code .


It also helps in name confusion of variables and methods. It encapulates the use of that variable or method within its capsule and provides access only with proper call so avoiding confusion .


Also while modification  can enforce some rules on in what way can the data be modied or not modified at all by not having any setters/mutators for the fields .


So far whatever I have understood encapsulation is like the boundary of a house ..every house has a some variables but other house don't interfere ..the variables are worked upon inside the house only ..  Also it gives the variables  boundary ...so it gives two things ...non-interferance/non-amguity and protection .. these two things may seem similar at times .. 


Encapsulation is a way we try to give boundary to our objects just like real life ..trying to imitate real life ...and whatever is inside is encapsulated ..it provides data hiding as well as protection of the object  from external interference ..which may mean the same thing .


...continued 


Shruthi Pillai
Shruthi Pillai
Information Technology
3mos ago

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. The data in a class is hidden from other classes, so it is also known as data-hiding.Encapsulation can be achieved by, Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. Encapsulation also improves the re-usability and easy to change with new requirements.