Encapsulation in java:
Encapsulation = Data Hiding + Abstraction.
It means if any component is following Abstraction as well as data hiding then it is also following Encapsulation.
It is actually the process of binding data members and methods together in a unit. Each class in java itself is an example of encapsulation. It is just like capsule medicine that encapsulate the medicines in a case(container) to bind the inner constituents into a unit to make it safe from misuse.
This is a process of keeping out internal data safe from unauthorized access. Means no one can access our internal data directly.
We can achieve data hiding programmatically by declaring data members as private, so that outside person can access our data through public method which will validate the user by asking username and password but can not access directly.
Suppose there is a bank in which many people accounts are there, and bank software has been implemented in a way that balance variable is defined as a public variable. So if you are having account in this bank, then your account balance is known to public so any one can know your account balance. Do you like it? Obviously NO.
So for making your account safe they declare balance variable as private so that anyone can access those private members only through methods defined inside that class, and this method will be asking you account holder name and password for authentication.
So by utilizing the concept of data hiding, security is achieved.
This is the mechanism to
- Hide the unnecessary things from user so providing easiness.
- Hiding the internal implementation of software so providing security.
Suppose you are having a mobile, it has information about two aspects.
1. user aspects
2. engineer aspects.
For a user, mobile manufacturer hides the Engineer aspects which is necessary only when you give your mobile to service center for repairing. So when you press numbers to make call you doesn’t care about what’s happening there inside after pressing buttons, you just enjoy calling, so in this way abstraction provides Easiness and Simplicity.
Now suppose you went to ATM, swiped your card to withdraw money. Some instructions to be followed displayed on the screen, you provide some info like PIN code of your account, again some options appeared on screen like withdraw, balance check. Now these things are happening inside, but you doesn’t know how queries are being generated based on your inserted info in ATM machine. After queries generation, to which database these queries are going, where that database is located, which database is being used whether Oracle or Mysql, which language has been used for these implementation like whether java or c or c++. You as a user not knowing anything about these info, so its tough to hack that machine, because you don’t have enough knowledge about internal implementation. You are using just UI(user interface) for your transaction. If you try to know about any information mentioned above from the bank people they may call the police by thinking that you are a hacker.
So actually I want to convey that in this way they provide easiness to user and security to systems.
Tags: java encapsulation, encapsulation in java, java interview questions, oops concept encapsulation, oops in java, what is encapsulation in java, encapsulation, java, java semaphore, semaphore in java, java static class, what does encapsulation mean, encapsulation in oops, code encapsulation