Difference between the concepts

android, ios, java, php

Solution

Encapsulation in layman's terms simply means covering(encapsulating).

With respect to Java it means writing your code together and exposing only those part of it which are intended to be exposed.But it is often associated with data hiding. For example when you define your instance method/function private it can be accessed only from the same class. This way you don't expose your instance variables/functions directly. Why? because user has nothing to do with it.

Abstraction again in layman's terms is a concept that is built up to hide the complexity behind it. Take for example computers. They have been an abstraction to processor which do the actual computation which inturn involved chips which inturn involves gates. For common man it would be difficult to talk in terms of gates used. SO the concept has been abstracted out to computers.

With respect to Java abstraction means hiding implementation details from the user. That includes making a class abstract or defining an interface. All user is exposed to is the interface(a set of functions or APIs). He need not know it's internal implementation. All he has to know what input he must provide(arguments) and what would be corresponding output(return type).

For better understanding with Java example refer to this question.

Problem

I have attended an interview for java programmer ,After a few personal questions ,the interviewer have asked me the question "What is encapsalution and abstraction" I was really happy with question ,and i have answered it as hiding the implementation details that are not necessary for the user to know. And Abstraction is showing of only the important details to user. only seconds after my answering ,the interviewer had another question for me. showing of only the important details to user ,that means hiding irrelevant data like implementation details to user. is it? I have replied it as yes!!! There comes another question from .So what is the difference between abstraction and encapsolution .I think there is no difference according to your answer. I was like i dont know ,My hands were frozen ,was a really bad day for me Can any one explain how would you answer if someone asks such a question to you

Original source

Related problems