Encapsulation in Java with Realtime Example and Advantages

In the previous article, we study Abstraction in Java. Here, we will open a new chapter and learn about the process of encapsulation in Java. Along with this, we will cover the advantages and real-time example.

Introduction to Encapsulation in Java

Encapsulation in Java with Realtime Example

2. What is Encapsulation in Java?

It defines as the process in which we wrap the data into a single unit. Java 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.
  • We can also call it, as Data-hiding.
  • It 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 Encapsulatio?

Encapsulation in Java - Introduction

Variable Data & Class

3. Advantages of Encapsulation in Java

These are benefits of Java Encapsulation:

  • Data Hiding – It can provide 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 store.
  • Flexibility – With this, 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.

4. Encapsulation in Java with Examples

i. To Demonstrate 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;

Recommended reading – What is Polymorphism in Java?

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);       

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

Java Quiz

5. Summary

Hence, we studied and learned all about Java Encapsulation with realtime examples and advantages. In addition, we discuss the demonstration and access variable of the class encapsulateDemo. Furthermore, if you feel any query, feel free to ask in a comment section. In our next article, you will learn Interface in Java.

2 Responses

  1. kol says:

    how is it easy to use a java

  2. STEPHEN says:

    Beautiful piece sir.
    Can we say
    string1. toLowerCase is an example of data encapsulation in Java?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.