Junilu Lacar wrote:Yes, you got it. Don't overthink it. They are both related but they're different perspectives.
Think of it like this: If you are a cashier at a store, abstraction is like you thinking about the customer paying for a purchase. That's the main behavior you care about. Implementation is whether the payment is done with cash, card, or check.
On the other hand, encapsulation is about the Customer knowing how much money they have in their bank account, how much cash they have in their wallet, or how much more they can charge before they reach their credit limit. To you, the cashier, that is information you are not privvy to, i.e. the information is hidden from you. Any attempt by you to obtain that information is inappropriate, i.e. you would be violating your customer's privacy or in ther words, you're breaking their encapsulation.
Does that make sense?
Thank you for the example.
Here is an example I came up with after doing a little more search.
Say you have a RAM class and a HDD class, the RAM uses the HDD to read and write files. If the HDD class was "well encapsulated" (the implementation details of how the HDD stores files and searches for them are hidden) then the RAM just use two methods, one to write to the HDD (say hdd1.writeFile(File f)) and another to read from (say hdd1.readFile(File f)) and that achieves the concept of abstraction, because the RAM class is not concerned with how the files are stored within the HDD, it just reads and writes to it using two methods (through the class interface), so the the implementation of HDD class is separated from the use of it.
However, if the HDD was not "well encapsulated" (its implementation details are visible to the client/user classes), then the RAM class will do something like this to read a file from the HDD :
In the preceding code we see that the implementation details of the HDD class are exposed (for example we know that the HDD class stores the files in an array), thus the concept of encapsulation is not fulfilled.
Also, we see that the use of the HDD class is not separated from the its implementation (when we use it we actually think of how its implemented in order to read a file), thus the concept of abstraction is not fulfilled, as well.
So, what we can conclude is that encapsulation is a requirement for abstraction. If there were no encapsulation (the encapsulation is violated), there would be no abstraction.
I hope you validate me on my understanding.
Thank you once again.