Scientech Easy Real-time Encapsulation Interview Questions & Answers: The Easy Way | Scientech Easy

Friday, June 15, 2018

Real-time Encapsulation Interview Questions & Answers: The Easy Way



encapsulation interview,oops interview questions and answers,java interview questions and answers

Real-time Encapsulation Interview Questions and Answers

In this tutorial, we will discuss Real-time Encapsulation Interview Questions and Answers with some practical examples. We will try to present answers in very easy ways so that you could not get any trouble to understand and remember the answers. 

1. What is Encapsulation? Why is it called Data hiding?
Ans: Encapsulation is a technique of making the fields private in the class and providing the access to the fields via the public method. 
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. Encapsulation 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 following advantages of Encapsulation:
An encapsulated code is more flexible and easy to change with new requirements.
Encapsulation prevents the other classes to access the private fields.
➝ Encapsulation allows modifying implemented code without breaking others code who have implemented the code.
➝ It keeps the data and codes safe from external inheritance.
➝ If you don't define setter method in the class then the fields can be made read-only.
➝ If you don't define getter method in the class then the fields can be made write-only.
➝ Encapsulation also helps to write the immutable class in java which is the good choice in multithreading.


4. What are the main benefits of using encapsulation in Java?
Ans: The main benefits of using encapsulation are:
➝ The main benefit of encapsulation is the ability to modify the implemented code without breaking the others code who have implemented the code. 
➝ It also provides us with maintainability, flexibility, and extensibility to our code. 
The fields of a class can be made read-only or write-only.
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:
Declare the variable of the class as private.
Provide public setter and getter methods to modify the values of variables.
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 the difference between Abstraction and Encapsulation?
Ans: Difference between Abstraction and Encapsulation:
Abstraction solves the problem at design level whereas 
    Encapsulation solves the problem at the implementation level.
Abstraction is implemented in Java using Interface and Abstract class whereas Encapsulation is implemented using private and protected access modifiers.
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 the access from outside.
The real-time example of Abstraction is TV Remote Button whereas 
    The real-time example of Encapsulation is medical medicine.

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

8. Give a real-time example of encapsulation in Java?
Ans: Suppose you have an account in the bank. If your balance variable is defined as a public variable in the bank software then your account balance will be known public then, in this case, anyone can know your account balance. So do you like it? Obviously No. 
   So for making your account safe, they declare balance variable as private so that anyone cannot see your account balance and the person who have to see account balance then they will have to access private members only through methods defined inside that class and this method will ask your account holder name or user Id, or password for authentication. 
Thus, security is achieved by utilizing the concept of data hiding. This is called Encapsulation.

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

10. 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:
I hope this article will help you to understand about the Real time Encapsulation Interview Questions and Answers in easy ways. All these questions are very important for interview purpose. First of all, you understand the concepts of Encapsulation and then go for interview questions. 

Popular