Encapsulation in Java with interview questions and answers with examples.

In this tutorial, we will discuss on Java encapsulation and at the end, we will focus on interview questions and answers with some practical examples. We trying to present Java concept in the very easy way so if you feel any trouble please comment below.

What is encapsulation in Java?

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

Wrapping code and data member in a single unit is the basic concept behind the 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 getter and setter method.

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 variable as a private and public getter and setter method to access this variable. So we are accessing variables indirectly so its make secure to our code.

class Main {
  public static void main(String[] args) {
    User u = new User();

    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 getter and setter method.

Some important point you should remember related to encapsulation in Java.

  • Encapsulation in Java is used to data hiding.
  • It is helpful to reuse the code.
  • Unit testing is very easy for an encapsulated code.
  • Encapsulated code easy to maintain like we can change in a code or class without affecting other code or class.
  • 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.

For more clear your doubt, please click here.

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 in detail.

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

What is the advantage of encapsulation in Java?

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


You May Also Like These Posts:

Leave a Reply

Your email address will not be published.

two × two =