Java - Interview Questions and Answers on Encapsulation

Q1.  What is difference between Encapsulation And Abstraction?


1.Abstraction solves the problem at design level while encapsulation solves the problem at implementation level

2.Abstraction is used for hiding the unwanted data and giving relevant data. while Encapsulation means hiding the code and data into a single unit to protect the data from outside world.

3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of how an object does something.

4.For example: Outer Look of a Television, like it has a display screen and channel buttons to change channel it explains Abstraction but Inner Implementation detail of a Television how CRT and Display Screen are connect with each other using different circuits , it explains Encapsulation.

Q2.  Difference between Encapsulation and Data Hiding ?

Ans. Data Hiding is a broader concept. Encapsulation is a OOP's centri concept which is a way of data hiding in OOP's.  

Q3.  What are the features of encapsulation ?

Ans. Combine the data of our application and its manipulation at one place. 

Encapsulation Allow the state of an object to be accessed and modified through behaviors.

Reduce the coupling of modules and increase the cohesion inside them.

Q4.  Which of the following Java feature promotes access protection or Hiding ?

 a. Inheritance
 b. Encapsulation
 c. Abstraction
 d. Composition

Ans. Encapsulation