Skip to main content

Encapsulation in JAVA, a Brief explanation

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.
 #knowledgestreams.blogspot.com

Types of Data Handler Method
  1. Getter Method: It is used to return the value of private data members present in class.
  2. 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:

  1. public
  2. private
  3. default
  4. protected

 #knowledgestreams.blogspot.com

1Public

  • 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.

2Private

  • 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

  1. Make the Class public.
  2. Make the data members private.
  3. Every data member must have a public getter and setter methods.

Advantages of Encapsulation

  1. We can achieve Data Hiding.
  2. We can perform Data validation.
  3. 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?

  1.  A Bean Class must be a public non-abstract class.
  2. The data members present inside the bean class must be of a private access modifier.
  3. Every data member must have a public getter and setter method.
  4. 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


#java #encapsulation #post, #blog #knowledge

Comments

Popular posts from this blog

Weekly challenge 3 data analyst google professional certificate

1 . Question 1 The manage stage of the data life cycle is when a business decides what kind of data it needs, how the data will be handled, and who will be responsible for it. 1 / 1  point True False Correct During planning, a business decides what kind of data it needs, how it will be managed throughout its life cycle, who will be responsible for it, and the optimal outcomes. 2 . Question 2 A data analyst is working at a small tech startup. They’ve just completed an analysis project, which involved private company information about a new product launch. In order to keep the information safe, the analyst uses secure data-erasure software for the digital files and a shredder for the paper files. Which stage of the data life cycle does this describe? 1 / 1  point Archive Plan Manage Destroy Correct This describes the destroy phase, during which data analysts use secure data-erasure software and shred paper files to protect private information. 3 . Question 3 In the analyze phase of the d

Prepare Data for Exploration: Weekly challenge 4

Prepare Data for Exploration: Weekly challenge 4 1 . Question 1 A data analytics team labels its files to indicate their content, creation date, and version number. The team is using what data organization tool? 1 / 1  point File-naming verifications File-naming references File-naming conventions File-naming attributes Correct 2 . Question 2 Your boss assigns you a new multi-phase project and you create a naming convention for all of your files. With this project lasting years and incorporating multiple analysts it’s crucial that you create data explaining how your naming conventions are structured. What is this data called? 0 / 1  point Descriptive data Named convention Metadata Labeled data Incorrect Please review the video on naming conventions . 3 . Question 3 A grocery store is collecting inventory data from their produce section. What is an appropriate naming convention for this file? 0 / 1  point Todays_Produce Produce_Inventory_2022-09-15_V01 Todays Produce 2022-15-09 Inventory

Prepare Data for Exploration : weekly challenge 1

Prepare Data for Exploration : weekly challenge 1 #coursera #exploration #weekly #challenge 1 #cybersecurity #coursera #quiz #solution #network Are you prepared to increase your data exploration abilities? The goal of Coursera's Week 1 challenge, "Prepare Data for Exploration," is to provide you the skills and resources you need to turn unprocessed data into insightful information. With the knowledge you'll gain from this course, you can ensure that your data is organised, clean, and ready for analysis. Data preparation is one of the most important processes in any data analysis effort. Inaccurate results and flawed conclusions might emerge from poorly prepared data. You may prepare your data for exploration with Coursera's Weekly Challenge 1. You'll discover industry best practises and insider advice. #answers #questions #flashcard 1 . Question 1 What is the most likely reason that a data analyst would use historical data instead of gathering new data? 1 / 1