Encapsulation in Java – Advantages and Example 1

1. Objective

In the Previous tutorial, we study Abstraction in Java, in this tutorial, we will learn about the process of encapsulation in Java, advantages of Java Encapsulation and, encapsulation in Java with examples to understand the concept better.

So, let’s start Encapsulation in Java Programming Language.

Introduction to Encapsulation in Java

Encapsulation in Java – Introduction

2. What is Encapsulation in Java?

Before we define, Encapsulation in Java, lets’s revise Key features of Java.

Encapsulation in Java is defined as the process in which we wrap the data into a single unit. Encapsulation binds the data and code. It basically creates a shield and code cannot be accessed outside the shield or by any code outside the shield.

  • The variables in Java or the method of the class are hidden from any other class and cannot be accessed outside the class.
  • This process is called Data-hiding.
  • Java Encapsulation can be achieved by declaring the class as private while the methods as public so that the variables can be accessed.

Do you know the difference between Abstraction and Encapsulation

Encapsulation in Java - Introduction

Introduction- Encapsulation in Java

3. Encapsulation in Java with Examples

Let’s see some example of Java Encapsulation:

i. To Demonstrate Java Encapsulation

public class Encapsulate
    // private variables declared, these can only be accessed by public methods of class
    private String flairName;
    private int flairRoll;
    private int flairAge;
    // get method for age to access private variable flairAge
public int getAge()
      return flairAge;
    // get method for name to access private variable flairName
public String getName()
      return flairName
    // get method for roll to access private variable flairRoll
public int getRoll()
       return flairRoll;
    // set method for age to access private variable flairage
public void setAge( int newAge)
       flairAge = newAge;
    // set method for name to access private variable flairName
public void setName(String newName)
      flairName = newName;
    // set method for roll to access private variable flairRoll
public void setRoll( int newRoll)
       flairRoll = newRoll;

ii. Access variables of the class EncapsulateDemo

public class TestEncapsulation
    public static void main (String[] args)
        Encapsulate obj = new Encapsulate();   
// setting values of the variables
// Displaying values of the variables
        System.out.println(" name: " + obj.getName());
        System.out.println(" age: " + obj.getAge());
        System.out.println(" roll: " + obj.getRoll());
// System.out.println("Geek's roll: " + obj.geekName);       

4. Advantages of Encapsulation in Java

These are benefits of Java Encapsulation:

  • Data Hiding – Encapsulation in Java, provides the programmer to hide the inner classes and the user to give access only to the desired codes. It allows the programmer to not allow the user to know how variables and data are stored.
  • Flexibility – With Java encapsulation, we can make the data as read-only or write-only as we require it to be.
  • Reusability – It allows the user to a programmer to use the program again and again.
  • Testing of the code – Ease of testing becomes easy.

So, this was all about Java Encapsulation Tutorial. Hope you like our explanation.

5. Conclusion

In this Java tutorial, we learned what encapsulation in Java, its real-time examples, and advantages of encapsulation in Java. Furthermore, if you feel any query, feel free to ask in a comment section.

See Also- Java Data Types & Java Operators

For reference

One thought on “Encapsulation in Java – Advantages and Example

Comments are closed.