It is a process of wrapping up the data members along with related data handlers method is called Encapsulation.
Wrapping up the data members and data handlers method means we are grouping both as a single unit. In Java, we group or wrap both inside the Class, so Class is a single unit that consists of data members as well as methods. As shown in the figure.
So, the class having both data members and related data handler method is called Encapsulated Class.
Here in the above definition, two important terms have been used i.e, data members, and data handlers method.
- Data members: The variables which are declared inside the class.
- Data handlers Method: It is just a usual method, whose only purpose is to initialized or return the value of data members present inside the Class. Such types of methods do not use for writing any business logic or functionality.
Types of Data Handler Method
- Getter Method: It is used to return the value of private data members present in class.
- Setter Method: The purpose of this method to initialize or override private data member’s value. With the setter method, you can modify the value of data members.
Don’t worry I explained in detail below.
Let us understand in detail,
In Java we have the concept of Class and Object, so inside the class, we declare some variables and methods, that variable or data members can be accessed easily which the help Object Reference of that class.
We can modify, initialize, and do all different types of operation on that data member.
Since we can directly do any type of changes or modifications with the value of data members, in short, we have the access to data members without any restrictions or without doing any validation of the user, this will give birth to a very big security breach. And you will land up in a horrible situation.
If your application contains confidential data, then Security is a very essential part, so to avoid security breach, it is highly recommended to declare your data members with private access modifiers.
And create a getter and setter method for each data member. So that no one can access directly instead they need to use a getter and setter method to access it.
Once the getter or setter method invoked or called then the very first line of code will perform validation, to identify the right user.
In the above paragraph, one new term has been used that is a private access modifier. Let’s have a quick look into the Access Modifier.
Access Modifier
It is a keyword in java which is used to decide the scope of classes, methods, or data members. Scope refers to visibility or from where you can able to access any of the members of Class which has some access modifier.
In Java, We have in total Four Access Modifiers:
- public
- private
- default
- protected
1. Public
- It is the highest visible access modifier in Java.
- It can be accessed globally, that is from within the class or outside the class or within the package or outside the packages.
- All the data members in Java can be declared with public access modifier.
2. Private
- It is the least visible access modifier in Java.
- The scope of private members is up to class level only that is private members can only be accessed from within the class and cannot be accessed outside the class.
- Private members cannot even be inherited.
- The outer class cannot be private but constructor, method, variables can be private.
3. Protected
- The scope of protected members is the package level.
- Protected variables and methods can be accessed outside of the package with the help of Inheritance.
4. Default
- Default is the automatic access modifier in Java, i.e, if any member is not declared as public, private, or protected then it automatically becomes the default.
- The scope of default member is package level, i.e, default members can only be accessed within the package and cannot be accessed outside the package.
- All the members in Java can have a default access modifier.
I hope you got some idea about Access Modifiers. Let’s come back to Encapsulation. I was talking about how to prevent the data members from accessing directly.
So for that, we need to make it private and provide getter and setter methods to access them.
Main Purpose of Encapsulation
The idea of making a data member as private, is that to provide security, so before accessing the data, there must be validation should perform for the login user.
So for this, we created a getter and a setter method, once either of the methods gets called or invoked, the very first step application will validate whether the login user is the right person to access this data, once the validation completed then the user can able to access the data.
In short, we can say, one more layer or wall is added named Validation before data members. Anyone who want to do modification or changes in the data member value, first they have to go through this validation process.
Encapsulation in Java with realtime example
When you input the PIN and press the Enter button, then a method got triggered or invoked, that method first validates your PIN, so once the validation completed.
It will redirect to the next screen where you have lots of options, such as Balance Enquiry, Amount Withdrawal and many more. Suppose you wanted to withdraw money, once you input your amount and press enter.
It will trigger one more method, that is setter method, where the first thing again it will validate to proceed further. Similarly for checking the balance, if you press the enter button, the getter method will get invoked and firstly it will also do the validation.
In short with the help of the Encapsulation we can achieve High-Level Security.
But here to achieve security, we are paying something. Have you noticed, each time the validation happened because of that the process will be a little bit slower. See the image for a better understanding.
Rules of Encapsulation
- Make the Class public.
- Make the data members private.
- Every data member must have a public getter and setter methods.
Advantages of Encapsulation
- We can achieve Data Hiding.
- We can perform Data validation.
- We can achieve high-level security.
Note: The Class which fulfills all above condition that class in Java we called Bean Class and Object created for that class is called Bean Object.
Explain the Java Bean Specification?
- A Bean Class must be a public non-abstract class.
- The data members present inside the bean class must be of a private access modifier.
- Every data member must have a public getter and setter method.
- A Bean Class must have a public default constructor.
Here one more important thing I would like to mention, Encapsulation is a combination of two things Data hiding and Abstraction.
How Encapsulation is a combination of two Data hiding and Abstraction?
Let us consider the same ATM example, above Since all logics such as once you press enter after putting the PIN, what method will be triggered you don’t know, and all these processes executed behind the door you don’t have any clue.
At the same time, you cannot access the data member directly, because it is made private. So in order access, you need a getter or setter method, in short, the data member is hidden behind the method. Hence data hiding has been achieved.
Difference between Abstraction and Encapsulation?
In Abstraction we simply hiding the internal implementation details which are not useful for the users, however, security is not a high priority.
You can easily know what is a thing happening behind the scene you are interested in. But in the case of Encapsulation, the highest priority is given to achieve security. No unauthorized person is allowed to access any kind of data.
Let’s understand with some Real World Example.
Consider four-wheeler vehicles, there are so many small parts assembled to form a complete structure of the vehicle.
Such as axle, engine, gasoline filter, and many more, but all are hidden under the bonnet/hood of the vehicle. The only important things such as steering wheel, seats, etc.
Here the hidden things are not restricted from users, even if you want you can easily see everything. And the parts which essential for the users that only exposed. This is nothing but in Java, it is called Abstraction
Although in case of Encapsulation, even if you are interested to know, logic or algorithm wrote for the application which you browse frequently such as Facebook or Google, then you unable to know as an EndUser, for that, you have joined that companies and be a part of the developer team.
Thus, we can say that Encapsulation is the combination of two terms that is Data Hiding as well as Abstraction.
Thanks a lot for reading my article. It’s highly appreciated. I hope you must learn something new from here. If you any quires or doubt please don’t hesitate to ask, I love to clear your all doubts.
And if you any suggestions regarding my posts Please let me know. If you want to learn some other concept of Java, please check out the articles list.