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

Cyber Attack Countermeasures : Module 4

 Cyber Attack Countermeasures :  Module 4 Quiz #cyber #quiz #coursera #exam #module #answers 1 . Question 1 CBC mode cryptography involves which of the following? 1 / 1  point Mediation of overt channels Mediation of covert channels Auditing of overt channels Auditing of covert channels None of the above Correct Correct! CBC mode is specifically designed to close covert communication channels in block encryption algorithms. 2 . Question 2 Which is a true statement? 1 / 1  point Conventional crypto scales perfectly well Conventional crypto scales poorly to large groups Conventional crypto does not need to scale All of the above Correct Correct! The symmetric key based method inherent in conventional cryptography does not scale well to large groups. 3 . Question 3 Public Key Cryptography involves which of the following? 1 / 1  point Publicly known secret keys Publicly known private keys Publicly known public keys All of the above ...

Cyber Attack Countermeasures : Module 2 Quiz

Cyber Attack Countermeasures: Module 2 Quiz #cyber #quiz #course #era #answer #module 1 . Question 1 “Identification” in the process of authentication involves which of the following? 1 / 1  point Typing a password Keying in a passphrase Typing in User ID and password Typing in User ID None of the above Correct Correct! The definition of identification involves providing a user’s ID (identification). 2 . Question 2 Which of the following statements is true? 1 / 1  point Identifiers are secret Identifiers are not secret Identifiers are the secret part of authentication All of the above Correct Correct! Identifiers for users are generally not viewed by security experts as being secret. 3 . Question 3 Which of the following is not a good candidate for use as a proof factor in the authentication process? 1 / 1  point Making sure the User ID is correct Typing in a correct password Confirming location, regardless of the country you are in The move...

Rectangular Microstrip Patch Antenna

Microstrip is a type of electrical transmission line which can be fabricated using printed circuit board technology, and is used to convey microwave-frequency signals. It consists of a conducting strip separated from a ground plane by a dielectric layer known as the substrate. The most commonly employed microstrip antenna is a rectangular patch which looks like a truncated  microstrip  transmission line. It is approximately of one-half wavelength long. When air is used as the dielectric substrate, the length of the rectangular microstrip antenna is approximately one-half of a free-space  wavelength . As the antenna is loaded with a dielectric as its substrate, the length of the antenna decreases as the relative  dielectric constant  of the substrate increases. The resonant length of the antenna is slightly shorter because of the extended electric "fringing fields" which increase the electrical length of the antenna slightly. An early model of the microst...