In this article Encapsulation in Java we give the information about Encapsulation simply means hiding the implementation details of the program from the end user and showing only some important information to the end user.

Encapsulation in Java:

Binding data members into a single object is called encapsulation. Encapsulation class has a protection mechanism to protect the defined properties & methods. With the help of encapsulation mechanism, we define access restrictions on class members as per our need, so that sensitive data cannot be accessed from outside.

Encapsulation simply means hiding the implementation details of the program from the end user and showing only some important information to the end user. Data is protected from external access only through encapsulation. In which class properties are defined as private and those properties are updated / modified through public methods.

In Java programming language, we create a fully encapsulated class by defining data members as private. Then setter and getter methods are used to set and get the data.

How to achieve encapsulation

Step 1. By declaring all the variables of the class as private

Step 2. Define public getter and setter methods to access or modify the value of the variable.

class Student

{

    private int id;

    private String name;

}

public class EncapDemo

{

     public static void main(String[] args)

     {

        Student p = new Student();

        System.out.println(“Student ID:” + p.id + “\nStudent Name:” + p.name);

    }

}

OutPut:

Compile time error

Explanation:

Above Program, we have defined the Student class, which has its variables ID and name and methods getId(), setId(), getname(), setname() all grouped together in one unit.

Here, we have declared the variables id and name as private so that no one can access them outside the class. This concept is called data hiding.

Since, here we have declared the variables as private, hence, when we tried to call them with the help of object, we got compile time error.

encapsulation program in java:

class Student

{

    private int id;

    private String name;

    public int getId()

    {

        return id;

    }

    public void setId(int s_id)

    {

        this.id = s_id;

    }

     public String getname()

     {

        return name;

     }

     public void setname(String s_name)

    {

        this.name = s_name;

    }

}

public class EncapDemo

{

   public static void main(String[] args)

  {

        Student s = new Student();

        s.setId(105);

        s.setname(“Raj”);

        System.out.println(“Student ID:” + s.getId() + “\nStudent Name:” + s.getname());

    }

}

Output:

Student ID: 105

Student Name: Raj

In the above program, we have also defined getter and setter methods for these two private variables id and name, which are public.

If someone outside the class wants to access the id and name, then he can access it only through getter and setter methods.

Encapsulation Vs Data Hiding

Often, people consider Encapsulation and Data Hiding to be the same, and often use them interchangeably, but this is not the case at all.

The function of encapsulation is to simply combine/group the data and its related methods together and keep it as a unit, which provides better management and security.

For example, you can relate it to your society, like there are many rooms in your society, there is a swimming pool, there is a gym, there is a guest-house, there is an audit room, etc.

Now who has connected all these together, your own society, so in this way your society is playing the role of encapsulation here.

Data hiding, on the other hand, restricts access to a data member by hiding its implementation details.

For example, the audit room, gym, swimming pool in your society cannot be used by anyone except your society members, only those who have been given permission by your society can use it.

NOTE: Encapsulation helps to achieve data hiding.

Data hiding is achieved by using access modifiers.

There are mainly 4 access modifiers in Java.

  1. public 2. Private 3. Protected 4. Default

Encapsulation bundles data together and keeps it as a unit, thus it also hides the data. It also makes the data private, so that the data cannot be accessed outside the class.

Along with data hiding, it provides only relevant details to the end-user, without exposing implementation details, which is called abstraction.

In this way we can say that encapsulation is a combination of both abstraction and data-hiding.

Encapsulation = data hiding + abstraction

encapsulation in java example:

public class Employee

{

  /*private properties to restrict access*/

  private String name;

  /*getter method to get data.*/

  public String getName()

  {

          return name;

  }

  /*setter method to set data.*/

  public void setName(String newName)

  {

          this.name = newName;

   }

}

public class Test

{

  public static void main(String[] args)

 {

    Employee empObj = new Employee();

    // set data using setter method.

     empObj.setName(“YashRaj”);

    // now get data using getter method.

     String name = empObj.getName();

    System.out.println(“Name : “+ name);

  }

}

Output

javac Test.java

java Test

YashRaj

Advantage of Encapsulation in Java:

  1. Through getter and setter method, we can make any class read-only or write-only.
  2. Through this we can control the data
  3. It helps in achieving data hiding.
  4. It protects the data from unauthorized access i.e. provides security.
  5. Since it groups data and methods together, it makes the code easier to manage and maintain.
  6. It helps in code enhancement, in future if we want to change the implementation part of any method, we can do it easily, without modifying any other method.
  7. Reduces Code Complexity :- Now since class members are bound into a single unit using access modifiers, code complexity also reduces.
  8. Data Security:- By using access modifiers on Class properties/Methods, the scope of members can be decided as per the need.
  9. Code Reusability:- Code re-usability also remains, you do not have to rewrite the code to perform the same operation.

Disadvantage of Encapsulation in Java:

  1. The length of the code increases because validation code has to be implemented at many places.
  2. Performance becomes slow because execution takes more time.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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