Java interview questions and answers on encapsulation

What is encapsulation in java? How to achieve encapsulation in a java program and Java Programming examples using encapsulation in Java.

In this Java Encapsulation tutorial, we will write some simple examples to explain Encapsulation in java and follow by some important Java interview questions and answers on encapsulation with Java programs using encapsulation.

What is encapsulation in Java?

Encapsulation is another concept of object-oriented programming language. As you know Java is an object-oriented programming language so encapsulation is also supported by java same as other oops concepts like class, object, inheritance, and polymorphism.

Wrapping code and data members in a single unit is the basic concept behind encapsulation. We can be achieved  Encapsulation by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. this method is called the getter and setter method.

How to achieve encapsulation in Java?

Let’s take an example and try to make it easy for you.

public class User {

  private String cb_name;
  private int cb_age;
  private int cb_salary;
public String getCb_name() {
  return cb_name;
}
public void setCb_name(String cb_name) {
  this.cb_name = cb_name;
}
public int getCb_age() {
  return cb_age;
}
public void setCb_age(int cb_age) {
  this.cb_age = cb_age;
}
public int getCb_salary() {
  return cb_salary;
}
public void setCb_salary(int cb_salary) {
  this.cb_salary = cb_salary;
}

  

}

In this User class, we define all the variables as private and public getters and setter methods to access this variable. So we are accessing variables indirectly so it makes secure to our code.

class Main {
  public static void main(String[] args) {
    User u = new User();
    u.setCb_name("Codebun");
    u.setCb_age(24);
    u.setCb_salary(50000);

    System.out.println(u.getCb_name() + " " + u.getCb_salary() + " " + u.getCb_age());
  }
}

In this code, you can see how to set and get the value using the getter and setter method.

Some important points you should remember are related to encapsulation in Java.

  • Encapsulation in Java is used for data hiding.
  • It is helpful to reuse the code.
  • Unit testing is very easy for an encapsulated code.
  • Encapsulated code is easy to maintain like we can change in a code or class without affecting other code or classes.
  • Encapsulation helps to make our code secure.

Java encapsulation interview questions and answers with the example.

Difference between Encapsulation and Data Hiding?

Data encapsulation is a process, while data hiding is both a process and a technique. They both share the category of object-oriented programming or in other words, you can say encapsulation is the way of data hiding.

What is the difference between Encapsulation And Abstraction?

Abstraction is a process of hiding the implementation details and showing only the functionality of the application read more in detail.

Encapsulation is the process of wrapping the code and data members in a single unit.

What is the advantage of encapsulation in Java?

  • Data hiding.
  • Easy to test (unit testing).
  • Security.
  • Reusability of the code.

Java programs on Encapsulation.