File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes encapsulation & abstraction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "encapsulation & abstraction" Watch "encapsulation & abstraction" New topic

encapsulation & abstraction

tushar limaye

Joined: Jan 14, 2001
Posts: 4
what is the difference between encapsulation and abstraction?
Jason Stortz
Ranch Hand

Joined: Jan 11, 2001
Posts: 68
Originally posted by tushar limaye:
what is the difference between encapsulation and abstraction?

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?

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.

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.
I agree. Here's the link:
subject: encapsulation & abstraction
It's not a secret anymore!