What is Encapsulation? Why do we do it?
Answers
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
Therefore, it is also known as data hiding.
To achieve encapsulation in Java −
•Declare the variables of a class as private.
•Provide public setter and getter methods to modify and view the variables values.
Thanks!
" Encapsulation is the wrapping mechanism which implements the concept of abstraction and prevents the data from unauthorized access.
Encapsulations used to prevent the data from unauthorized/ invalid access by implementing proper wrapping mechanisms on the data.
Encapsulation is also used to hide the complexity of a system/module to make things easier for the user or to make the debugging/development easier for the programmer.
Encapsulation allows the development of secure and modular applications."