Sarmishta Chaitanya2,204 Points
Need clarification - Encapsulation and Abstraction
There's always a confusion in my mind between encapsulation and abstraction. Both seem very similar to me. Can someone please clearly explain the difference between these two concepts with few examples?
Yanuar Prakoso15,191 Points
Allow me to explain it in my way. In my opinion abstraction and encapsulation are related but often distinctive in terms of point of view. Abstraction is more of customers or in this case users point of view and encapsulation is how to make such abstraction. Okay before it gets confusing let us just review the Pez Dispenser in the video.
For the users when they order PEZ they expect to have PEZ with head, which has character head chosen by them, and dispenser filled full by candies. They do not want to know how but they know that they will get exactly those specification. This is abstraction.
The other party involved here is the developers. The developers knows that the users expect to have PEZ with character head of their choice and dispenser full of candies. The users do not need to know how the developers made it. Therefore to achieve this kind of abstraction the developers use technique called encapsulation which provide a mechanism to restrict access to method therefore hiding the means of how it is done to the users. Once the code compiled the users will not have access to see the source code on how the method is done.
Thus when the developers encapsulate the part how they fill the dispenser with candies to its' maximum capacity the users will not get to see how it is done. The developers only ensure the users that they will get the job done. Therefore the users will get their PEZ with character head of their choice and dispenser filled with candies to the maximum capacity.
I hope this will help. Any other inputs are welcomed.