Originally posted by tushar limaye: what is the difference between encapsulation and abstraction?
Tushar, Hmm. Okay, here we go. First, lets deal with encapsulation, then we'll deal with abstraction, then we'll point out specifically the differences. Okay?
Encapsulation: The process of hiding data is called encapsulation(and so far this might sound a lot like abstraction). Let's take a small segment of code for an example:
Now, lets examine the "properties" of the BankAccount object. The first one is a private int called accountNumber. Here comes encapsulation! This property(an int) is encapsulated by the BankAccount Object. Only BankAccount has direct access to it, other objects can access it throuh the provided methods, but only BankAccount can directly make use of it. Now, amountOfMoney on the other hand is directly useable by BankAccount and any class it subclasses because it is protected and not private. The String "owner" is unencapsulated because we've declared it public and it can be accessed directly by using the "dot" operator like so:
This is not a good thing! Usually anyway, I suppose there will always be exceptions to the rule, but normally we do not want to do this. So, the short answer, encapsulation is used to make sure that the object is "never" put in an improper state by controlling access to it's properties. What this means is that instead of using the owner String via the "dot" operator true encapsulated programming would let you make that variable protected or private and use methods to access it. Now, why do we want to do this? So we can perform checks on incoming data and validate it. Make sure the "owner" has a first and last name for example. So data encapsulation is basically keeping controlled access to objects properties.
Abstraction: I once read in a book that the process of abstraction is a way of "seeing the forest through the trees." Abstraction sounds strange but this is a VERY COOL FEATURE. Think of studying mathematics(don't worry this won't get ugly). If I have the simple function of f(x)=1+x this is what happens: I give the function x=2, I get back 3. I give the function x=4, I get back 5. You can think of the function as a "black box", I give the box a 2 it spits out a 3 on the other side. At this point as a programmer I don't know and don't care HOW it does it, just that when I give it a 4 it gives me back a 5. Someone else already built the black box for me and I just want to use it. So, lets go back to our BankAccount. Let us say someone else wrote the BankAccont class for you. You know that the setOwner method call now checks for a first and last name. Abstraction says that you know it does this, but not how, and you don't care how. You get all the power of the code without needing to worry about how it does everything. So, maybe there is a new method called dropAccount in our BankAccount. Someone else programmed it and the description they give you is that it will pull the money out and cut a check to the owner of the account and then cancel the account. So, now you know what it does if you ever need to call that method, but you never know how it does it. You don't need to know that it verifies all data against a database call with JDBC and then transfers the account to the canceling desk, notifies a teller to make the check out, notifies the accounting center to update the banks account, notifies the owner via mail that the account is closed, etc.
SOOO, encapsulation is an Object "encapsulating" or wrapping itself around it's "properties" and providing controlled access to those properties, while abstraction is stepping back from from the smaller parts to get the bigger picture. I hope all this helps. Jason
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad: