12 Real Java Encapsulation Interview Questions Answers

Last Updated on March 30, 2020 by Scientech Easy

1. What is Encapsulation in Java? Why is it called Data hiding?
Ans: The process of binding data and corresponding methods (behavior) together into a single unit is called encapsulation in Java. In other words, encapsulation is a programming technique that binds the class members (variables and methods) together and prevents them from being accessed by other classes, thereby we can keep variables and methods safes from outside interference and misuse.

If a field is declared private in the class then it cannot be accessed by anyone outside the class and hides the fields within the class. Therefore, Encapsulation is also called data hiding.

2. What are the features of Encapsulation?
Ans: Encapsulation means combining the data of our application and its manipulation in one place. It allows the state of an object to be accessed and modified through behavior. It reduces the coupling of modules and increases the cohesion inside them.

3. What are the advantages of Encapsulation?
Ans: There are the following advantages of Encapsulation:
1. The encapsulated code is more flexible and easy to change with new requirements.
2. It prevents the other classes to access the private fields.
3. Encapsulation allows modifying implemented the code without breaking others code who have implemented the code.
4. It keeps the data and codes safe from external inheritance. Thus, Encapsulation helps to achieve security.
5. It improves the maintainability of the application.

4. What are the main benefits of using encapsulation in Java?
Ans: The main benefits of using encapsulation are:
1. The main benefit of encapsulation is the ability to modify the implemented code without breaking the others code who have implemented the code. 
2. It also provides us with maintainability, flexibility, and extensibility to our code. 
3. The fields of a class can be made read-only or write-only.
4. A class can have total control over what is stored in its fields.

5. How to achieve encapsulation in Java? Give an example.
Ans: To achieve the encapsulation in Java:
1. Declare the variable of the class as private.
2. Provide public setter and getter methods to modify the values of variables.
For example: 
    public class EncapsulationTest private String name; private String idNum; private int age; public int getAge(){ return age; } public String getName(){ return name; } public String getIdNum(){ return idNum; } public void setAge( int newAge){ age = newAge; } public void setName(String newName){ name = newName; } public void setIdNum( String newId){ idNum = newId; } }

6. What is data hiding in Java?
Ans: An outside person cannot access our internal data directly or our internal data should not go out directly. This oops feature is called data hiding n Java. After validation or authentication, the outside person can access our internal data.

7. How to achieve Data hiding programmatically?
Ans: By declaring data members (variables) as private, we can achieve or implement data hiding. If the variables are declared as private in the class, nobody can access them from outside the class. The biggest advantage of data hiding is we can achieve security.

8. What is Tightly encapsulated class in Java?

Ans: If each variable is declared as private in the class, it is called tightly encapsulated class in Java. For tightly encapsulated class, we are not required to check whether class contains getter and setter method or not and whether these methods are declared as public or not.

9. What is the difference between Abstraction and Encapsulation?
Ans: There are the following differences between Abstraction and Encapsulation:
1. Abstraction solves the problem at the design level whereas encapsulation solves the problem at the implementation level.
2. Abstraction is implemented in Java using Interface and Abstract class whereas encapsulation is implemented using private and protected access modifiers.
3. Abstraction is used to hide the unwanted data and giving relevant data whereas encapsulation is used for hiding data and code in a single unit to prevent access from outside.
4. The real-time example of Abstraction is TV Remote Button whereas the real-time example of Encapsulation is medical medicine.

10. What are the getter and setter methods in Java?
Ans: In Java, setter method is a method which is used for updating the values of a variable. This method is also known as mutator method. 

Getter method is a method which is used to retrieve the value of a variable or return the value of the private member variable. This method is also known as an accessor method.

11. Explain design pattern based on encapsulation in java?
Ans: Many design pattern  Java uses encapsulation technique and one of them is Factory pattern which is used to create the objects. Factory pattern is a better choice in creating the object of those classes whose creation logic can vary. It is also used for creating different implementations of the same interface. 

 ‘BorderFactory class’ of JDK is a good example of encapsulation in Java which creates different types of ‘border’ and encapsulates creation logic of border.

12. How can the variable of the EncapsulationTest be accessed by using getter and setter methods?

Ans: The public setXXX() and getXXX() methods are access points of the instance variable of EncapsulationTest class. Basically, these methods are known as getter and setter methods. Therefore, any class that wants to access variable should access them through these getters and setters. The variables of the EncapsulationTest class can be accessed  as shown below:

    public class RunEncapTest{ public static void main(String args[][]){ EncaptulationTest encap=new EncapsulationTest(); encap.setName(“John”); encap.setAge(22); encap.setId(123456); System.out.println(“Name: ” +encap.getName()); System.out.println(“Age: ” +encap.getAge()); System.out.println(“Id: ” +encap.getId()); } } This would produce following output: Name: John Age: 22 Id: 123456

Final words
Hope that this article will have helped you to understand real-time encapsulation interview questions and answers. All these questions are very important for interview purpose.
Thanks for reading!

Leave a Comment