Oct 8, 2015

Abstraction vs Encapsulation

Encapsulation means we are hiding the code and data in to a single unit to protect the data from outside world.
Abstraction means we know about the object how it can be used but we dont know how it is implemented. and we can hide the data using the AccessSpecifiers.

Data abstraction in OOP is the concept of abstracting the implemetaion to the user. It just gives the functional description but not the implementaion part.
Take an abstract class or an interface for example, it gives the user some method descriptions, but not the internal implementaion.  



While Encapsulation in OOP is the process of hiding or securing data from outside functions. The data is strictly binded with function members declared in the same class.
For example in C# we can use properties inside the class to manipulate or access the private data declared in the same class, but cannot do so from outside functions.


I think they are slightly different concepts, but often they are applied together.
 Encapsulation is a technique for hiding implementation details from the caller, whereas abstraction is more a design philosophy involving creating objects that are analogous to familiar objects/processes, to aid understanding. Encapsulation is just one of many techniques that can be used to create an abstraction.

For example, take "windows". They are not really windows in the traditional sense, they are just graphical squares on the screen. But it's useful to think of them as windows. That's an abstraction.
If the "windows API" hides the details of how the text or graphics is physically rendered within the boundaries of a window, that's encapsulation.

No comments:

Post a Comment