Encapsulation in Java with Realtime Example and its Advantages

Generally, the meaning of Encapsulation is the action of enclosing something. But in Java, Encapsulation is the wrapping up of data & function under a single unit. We can achieve this OOPs feature by declaring all the variables in the class as a public and private method, which will set and get the value of variables.

Programmers get many benefits of using encapsulation their coding. Let’s study how to implement Encapsulation in Java with a real-time example.

What is Encapsulation in Java?

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

  • 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.
  • The encapsulated class is easy to test.
  • Standard IDE’s like Eclipse, Netbeans are providing the facility to generate getter setter methods, so it is very easy to create an encapsulated call
  • 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 Java Abstraction and Encapsulation?

Java Encapsulation with real-time example

Master the concept of Polymorphism in Java in just 7 Mins.

Encapsulation in Java with Examples

Let us understand the concept of Java Encapsulation with the following example-

package com.dataflair.encapsulation;
class Encapsulation
{
  private String dataflairianName;
  private String dataflairianProfile;
  private int dataflairianAge;

  public String getDataflairianName() {
    return dataflairianName;
  }
  public void setDataFlairianName(String dataflairianName) {
    this.dataflairianName = dataflairianName;
  }
  public String getDataflairianProfile() {
    return dataflairianProfile;
  }
  public void setDataFlairianProfile(String dataflairianProfile) {
    this.dataflairianProfile = dataflairianProfile;
  }
  public int getDataflairianAge() {
    return dataflairianAge;
  }
  public void setDataFlairianAge(int dataflairianAge) {
    this.dataflairianAge = dataflairianAge;
  }
}
public class EncapsualtionDemo 
{
  public static void main (String[] args)
  {
    Encapsulation obj = new Encapsulation(); 
    // setting values of the variables
    obj.setDataFlairianName("Renuka Peshwani");
    obj.setDataFlairianAge(24);
    obj.setDataFlairianProfile("Digital Marketing Executive"); 
    // Displaying values of the variables
    System.out.println(" Name: " + obj.getDataflairianName());
    System.out.println(" Age: " + obj.getDataflairianAge());
    System.out.println(" Profile: " + obj.getDataflairianProfile());
  }
}

In the above code, we have three variables dataflairianName, dataflairianProfile, and dataflairianAge. With the help of getter and setter, we have created methods for all the variable and we have used them. Getter gets the value and setter sets the value of the variable.

Output-Encapsulation-program-in-java

Advantages of Encapsulation in Java

These are benefits of Encapsulation in Java:

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

It’s the right time to uncover the secret of Inner classes in Java

  • Getter and Setter Methods – Private member can only be accessed within the same class. An outside class can not access the data members of that class. If you need to access these variables, you have to use public “getter” and “setter” methods.
  • Flexibility – With this, we can make the data as read-only or write-only as we require it to be. It also improves the maintainability and flexibility of code.
  • Reusability – It allows the user to a programmer to use the existing code again and again in an effective way.
  • Testing of the code – Ease of testing becomes easy. So it is better for Unit testing
Java Quiz

Summary

Encapsulation in Java is one of the important concepts of OOPs. This feature gave us the concept of Data hiding. Every beginner should know how to work with encapsulation. Hope, this article helps you to clear your doubt.

Furthermore, if you feel any query, feel free to ask in a comment section.

In our next article, you will learn the 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.