Encapsulation in Java

Read Time:8 Minute, 39 Second

Table of Contents

One of the four pillars of the OOP concept is encapsulation. Inheritance, polymorphism, and abstraction are the other three.

In Java, encapsulation combines data (variables) and code that acts on the data (methods) into a single unit. Encapsulation means that a class’s variables are concealed from other classes and can only be accessed through its current class’s methods. Consequently, it’s also known as data concealment.

The goal of encapsulation is to keep implementation details hidden from users. Other members of the same class can only access a private data member. No outside class has access to a class’s private data member (variable).

However, suppose we create public getter and setter methods to update (for example, void setEmployee(int ID)) and read (for example, int getEmployee()) the private data fields. In that case, the outside class can use those public methods to access those private data fields. Private fields and their implementation are hidden from outside classes since public methods can only access data.

Encapsulation in Java

To implement encapsulation in Java,


  • Declare a class’s variables as private.
  • To alter and inspect the variables’ values, provide public setter and getter methods.

Following that, we’ll look at the syntax you use when implementing encapsulation in Java.The syntax is as follows:

Let’s explore the sample program below to understand the encapsulation process better.

Example: The following code illustrates how to implement Encapsulation in Java.

The public setXXX() and getXXX() methods provide access to the CodeEncapsulation class’s instance variables. These methods collectively are commonly referred to as getters and setters. As a result, any class that needs access to the variables should use these getters and setters. The CodeEncapsulation class variables are accessed with the following program:

Methods of Getter and Setter


A getter method is a method that allows you to access and retrieve an instance of a private variable.



The setter method is capable of updating or setting a private variable instance.

Encapsulation’s Advantages

  • You can make a class read-only or write-only by giving only a setter or getter method.
  • In other words, the getter and setter methods are optional.
  • The most common IDEs provide the ability to generate getters and setters. Creating an enclosed class in Java is thus simple and quick. By extension, this eases the entire process of writing programs.
  • Encapsulation in Java allows you to reuse your code.
  • Encapsulation allows you to make modifications to existing code swiftly.
  • It gives you complete control over your data. You can write the logic inside the setter method if you only want to set the value of id to be greater than 100. You can implement logic in the setter methods to prevent negative integers from being stored.
  • A class can have complete control over the content of its fields.
  • Because other classes will not be able to access the data through the private data members, it is a way to achieve data hiding in Java. It’s simple to test the enclosed class. As a result, it is more suitable for unit testing.
  • Unit testing code written with encapsulation is simple.

Encapsulation in Java: A Simple Example

Let’s look at a simple encapsulation example with only one field and setter and getter methods.

The following is a read-only class.

You can no longer edit the value of the code_school data member “Code Underscored .”

The following is a write-only class:


You can no longer access the code_school’s value; instead, you can only update the value of the code_school data component.

Another Java example of encapsulation

Let’s look at another encapsulation example with only four fields and setter and getter functions.

Example: Encapsulation in Java

We established a class called CodeArea in the previous example. The class’s primary goal is to compute the area. Two variables, c_length, and c_breadth, plus a method, getCodeArea, are required to determine the area. As a result, we combined these attributes and methods into one class.

The properties and methods of this class can also be accessible from other classes. As a result, this is not data concealment. It is only a summary. We’re just grouping comparable codes. While many people think of encapsulation as data hiding, this is not totally accurate. Encapsulation is the grouping together of similar fields and procedures. You can use it to conceal information. Data hiding is, therefore, not the same as encapsulation.

What is the purpose of encapsulation?

Encapsulation in Java allows us to group relevant fields and methods, making our code clearer and easier to read.


It aids in the management of our data fields’ values. For instance,

The c_age variable is now private, and logic is applied inside the setCodeAge() method. c_age is no longer a negative factor. Our class fields can exist as read-only or write-only. The latter is dependent on either using the getter and setter methods. For instance,

It aids in the decoupling of system components. We can, for example, divide code into multiple bundles. These detached components (bundle) can be built, tested, and debugged on their own time. Furthermore, any changes made to one component have no impact on the others. Encapsulation can also be used to conceal data. If we set the length and breadth variables to private in the previous example, access to these fields is restricted. They are also kept secret from the upper classes. It is referred to as data concealment.

Hiding Data

Data hiding is a method of limiting data members’ access to our data by concealing implementation details. Encapsulation also allows data to be hidden. You can hide data by using access modifiers.

The following are the four access specifiers:



The first line of data concealing is the default. In case no access specifier is explicitly specified for a given Java class, the compiler will use ‘default’ as the access specifier. The default access specifier is very similar to the public access specifier in terms of functionality.


The least restrictive access modifier is this. Methods and properties with the public modifier can be accessible both within and outside of your current class.

The public API of your class and any component you include includes public methods and attributes. It is usually never a good idea for any attribute. Therefore use caution when applying this modification to a method. When a method is made publicly available, it must be adequately described and capable of handling input values. Remember that this technique will be utilized by a portion of your program, making it difficult to update or eliminate.

Your public API should, in general, be as light as feasible. Only the methods used by other portions of the program or external clients are included in public APIs. The public access specifier gives class access to its access specifications for access from anywhere in the program.




The private modifier, the most restrictive and widely used access modifier, restricts access to an attribute or method to other members of the same class. This attribute or method is not accessible to subclasses or other classes in the same or different package.

By default, choose the private modifier for all attributes and internal methods you should not call from outside classes. You may need to make an exception to this rule when using inheritance. Also, some subclasses that require direct access to a property or internal method should be exempted. Instead of using the private modifier, you should use the protected modifier. The data members use the private access specifier for accessibility. On the other hand, the data methods are limited to the class itself.



Like the private access specifier, the protected access specifier safeguards the class methods and members. The main distinction is that instead of restricting access to a single class, the package as a whole is restricted. Internal methods that must be called or overridden by subclasses are usually given the protected modifier. You can also use the protected modifier to give subclasses direct access to a superclass’s internal attributes.

Example: Data hiding

We have a private field c_age in the example above. You cannot access it from outside the class because it’s private. In this example, we utilized the public methods getCodeAge() and setCodeAge() to retrieve c_age. These are known as getter and setter methods. We were able to prohibit illegal access from outside the class by making age secret. This is data concealment. Attempts to access the c_age field from the Main class will get an error.


Encapsulation vs. Abstraction

Abstraction and encapsulation are frequently confused. Let us investigate-

  • Encapsulation is mainly concerned with the “How” of achieving functionality.
  • Abstraction is primarily concerned with “What” a class can accomplish.

A mobile phone is an easy way to understand the difference where the circuit board’s sophisticated circuitry is encapsulated in a touch screen, with an interface to abstract it away.

Example: Program for demonstrating variable access using encapsulation in Java


In Java, encapsulation combines code and data into a single unit, such as a capsule containing several drugs. Further, in Java, we can create a fully enclosed class by keeping all of the class’s data members private. We can now set and get data using setter and getter methods. The Java Bean class represents a fully contained class. It’s frequently used to implement a data-hiding method. This strategy minimizes the accessibility of properties to the current class and controls and restricts external access to these attributes using public getter and setter methods. These methods let you specify which characteristics can be read or updated and validate the new value before making a change.

Encapsulation has the basic property of hiding data and ensuring the security of user data. Encapsulation is a good OOP practice. However, it works best when combined with a reliable APM solution like Retrace for error monitoring.

Source: https://www.codeunderscored.com/encapsulation-in-java/