Java - Interview Questions and Answers on Abstraction

Q1.  What is difference between Encapsulation And Abstraction?

Ans. 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 Abstraction and Implementation hiding ?

Ans. Implementation Hiding is a broader concept. Abstraction is a way of implementation hiding in OOP's.

Q3.  What are the examples of Abstraction in Java ?


function calling - hides implementation details
wrapper classes 
new operator  - Creates object in memory, calls constructor 

Q4.  How is Abstraction implemented in Java ?

Ans. Abstraction is provided in Java by following ways -

Coding to the ( Interfaces / Abstract Classes ) or contracts

By Encapsulating details within classes and exposing the minimal Door ( few public methods )