Encapsulation vs Abstraction
I must admit. I am still having hard time seeing a real difference. I am still having hard time defining them both.
One thing I am sure: both are fundamental principles of object oriented programming. I am going to try to learn them here and explain the differences. Just so I know for future.
"Abstraction and encapsulation are not synomous," says P.J. Plauger in Programming on Purpose.
Encapsulation is information hiding. I think that's the best way to define it and remember it. "Information hiding allows for implementation of a feature to change without affecting other parts that are depending on it."
P.J. Plauger has a great analogy and definition of information hiding.
"Information hiding is not the same as secrecy. The idea is not to prevent outsiders from knowing what you are doing inside a module. Rather, it is to encourage them not to depend on that knowledge. That leads to a kind of secondary coupling which is more pernicious than obvios dependency because it is less visible. You should encapsulate information to keep it private, not secret. (What you do in the bathroom is no secret, but it is private.)"
"Stuff all of the code that is likely to change in a module [class] and hide its innards." That not only defines information hiding, but it is also a fundamental principle developing: when to create a new class, when to refactor.
Abstraction. Look at it as "what" the method or a module does not "how" it does it. I just found a great definition of abstraction: "The notion abstraction is to distill a complicated system down to its most fundamental parts and describe these parts in a simple, precise language."
Let's compare Java and C++. We have a good example of abstraction. In C++, you have to deal with pointers and references a lot. You have to deal a lot of garbage collection. In essence, you have to work on a low level. (C and C++ in turn abstracted a lot of even lower level machine code.) In Java, those things are abstracted away. You just assume they exist. In essence, abstraction means that you are working on a higher level. You don't care how pointers work in Java, you just assume that they work. You don't have to concentrate on the lower level stuff, you work on higher level.
I think abstraction is extremely important. By working more abstractly we can solve more and more difficult problems because we don't have to concentrate on the lower level details. As Steve McConnel puts it, we are working closer to the business domain.
So to summarize, I think I got it now. Encapsulation is information hiding. Abstraction means working on a higher level, not worrying about the internal details. They go hand in hand. The bottom line, even if you still don't know the differences, utilize information hiding; make your programs more abstract by creating classes with responsibility and then talk about the feature by the name of the class, but what it does, not by how it is doing it (that's abstraction, I think).
Comments
· May 15, 2006 ·
Now I am dam clear about the difference... Excellent article.
· May 21, 2006 ·
"Encapsulation is information hiding. Abstraction means working on a higher level, not worrying about the internal details." Encapsulation hides data and Abstraction hides implementation then what is the main difference. I think encapsulation is a data binding as information of one object is only related to that one and if that particular object gets destroyed then al information related to that object gets destroyed. Abstraction on the other hand is data hinding technique hides all implementation.
· September 5, 2006 ·
Chauhan,
Not sure what you mean. The way you explain it makes it even more confusing...
You want to think about your system on a higher level, using class names, using real objects. That's abstraction -- not having to concentrate on low-level details. For instance, when constructing a house, you don't concentrate on what nails will be used (low level detail, not enough abstraction), you concentrate on how many rooms, etc (high level -- nicely abstracted).
Encapsulation, in a way, is the implementation of abstraction. By encapsulating, you hide the internals of the method, or a class. Read the section of this article on encapsulation, "Information hiding allows for implementation of a feature to change without affecting other parts that are depending on it."
· September 6, 2006 ·
I think Abstraction is hiding unwanted details, like we have a customer object which uses a very complex data access layer to fetch the Customer from DataBase. Its only programmers headache to thin and implement that layer. There is no need for end user to nkow How this Customer is fetched from DB? Which Db is he using? What's the connection string like?....and all that complex stuff...All he knows is that, he wants to get information about XYZ Customer and he gets it by selecting his name from the Combo box and clicking 'Show Details' button.
On the other hand Encapsulation means , grouping together the data and operations that we are going to perform on that data together. This means creating a class with variables, properties and functions. We provide a specific interface using which user can manipulate this data. If we provide a function GetCustomerSalary(string CustomerName)...then to get a Customer's Salary .. user will allways have to provide Customer name as there is no other way he can do that (else provided by programmer).
Its simple you have a car and you know how to drive it. Just use the keys and turn them to start the car. What happens inside is not the drivers job. This is abstraction.
Once you are inside the car and you want to change the gear, you can only do that using the interface provided i.e. Gear Knob. There no other way you can do that. This is encapsulation.
· December 14, 2006 ·
What does it mean for a programming language to support abstraction? I thought if you are able to create a class and instantiate objects, then ur language supports abstraction. If you are able to assign private/public/protected modifiers, then ur language supports encapsulation. Now, from the discussion here, it seems like I got quite a few things wrong here.
Cant understand why things have to be so complex? Abstraction and Encapsulation are supposedly the pillars of OOP. If there is so much scope for confusion about the pillars themselves, how can we simplify complex systems using these?
· October 2, 2008 ·
Or is abstraction the ability to define an abstract method, class, interface? Sorry for pulling things down to the implementation level, but I need that to understand concretely.
· October 2, 2008 ·
Go to:
« previous entry: A Simple Design
» next entry:
Programming on Purpose -- P.J. Plauger
The Pragmatic Craftsman
I see that several people are searching for "abstraction" and "encapsulation."
Here are some additional good definitions:
http://homepages.ius.edu/gmanwani/C202/abstraction_means_concentrating_.htm