Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (javk):

In simple words, what is the difference between abstraction and encapsulation (in object oriented programming). I got 'it' for a while there, but then it slipped through my fingers, just like sand, and I couldn't do a thing about it.

OpenStudy (woodrow73):

This is a very good answer that I came across, referencing the two terms: http://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation "Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics." - Grady Booch, Object Oriented Analysis and Design For encapsulation, think of a pill (object) encapsulating / containing field variables and methods within it's 'instance'. To solidify that idea, think of a program for a restaurant to keep track of customers, each object encapsulates information, party 1 might have different orders, and different amount of money spent than party 2. I haven't heard abstraction used in this context before, but it makes sense that it's the 'idea', the exterior performance of an object. For instance, take a complicated object with complex methods and fields that perform machine learning abilities. The abstraction could be that the object can learn to perform a particular function, like image recognition. That's sort of how I see it.

OpenStudy (javk):

so this is what I understood so far...abstraction is the process of identifying what details need to be hidden so that we see what the object does, rather than how it does it. Since this is speaking about objects, this would then be mostly about classes, Whereas encapsulation is the actual deed of hiding information that would otherwise be irrelevant... now I am confused again

OpenStudy (javk):

Okay…so…from the textbook: `Abstraction to programmers, means trimming away unnecessary details. A thing is represented only by its most significant attributes.` And `Data hiding is where the irrelevant part of the internal representation of the object is hidden from the client ` That means, putting those two together, abstraction and data hiding are opposites and are both associated with encapsulation. When data is encapsulated, some of its significant attributes will be visible by means of abstraction, whereas the irrelevant internal representations will be hidden. Does that make any sense?

OpenStudy (woodrow73):

I'm no expert on the definitions, I guess an abstraction can be a process or an entity, going by an SO response "One point of confusion regarding abstraction is its use as both process and an entity. Abstraction, as a process, denotes the extracting of the essential details about an item, or a group of items, while ignoring the inessential details. Abstraction, as an entity, denotes a model, a view, or some other focused representation for an actual item." To me it sounds like abstraction as a process is similar to describing an algorithm, sort of enough to conceptually understand the inner workings, but not the semantics. I could be wrong though. Perhaps the terms 'essential details' actually talk about what methods and fields are accessable outside of the class/object. I can't be sure your last sentence makes sense because I'm not sure on the definitions of the terms; you might benefit from finding examples of the two terms. Good luck getting clarity.

OpenStudy (woodrow73):

Here's one interesting abstraction example: http://stackoverflow.com/a/11965960/4360854

OpenStudy (woodrow73):

The answer below the above link also refers to abstraction talking about class hierarchy, which sort of makes sense, considering the consequences of using the abstract keyword in java to make an abstract class. Identifying commonalities, and creating a class hierarchy where information in the subclasses can be 'abstracted'. If you haven't heard about abstract classes, they are sort of like super-classes that force their sub-classes to implement certain specified methods. And you can't instantiate an instance of an abstract class, but you can declare one like `SuperClass obj = new SubClass();` for a more full explanation: https://www.youtube.com/watch?v=CUC522qMGe8

OpenStudy (woodrow73):

I'm not positive whether using a class/object's getter method to return a field `obj1.getField1();` can be called abstraction or not, my hunch says no, but further investigation may be needed. Encapsulation also seems to be synonymous / interchangable with data hiding, as noted by some of the SO answers, though wikipedia notes some differ on that opinion. Whether encapsulation necessitates private field variables and only access via getter/setter methods or not doesn't end up making a difference in my opinion, access is access. Though jargon enforcers may beg to differ lol.

OpenStudy (woodrow73):

What the term encapsulation explicitly specifies aside, setters and getters are a more organized way to change important fields, especially in libraries, and classes with a lot of variables.

OpenStudy (theeric):

I'm with @woodrow73 in the boat of not knowing what the terms mean exactly. It seems like terms in computer science are flexible; I've heard that this might be because the field is rapidly evolving, there is no standard or single origin of terms, and lots of people use terms colloquially after picking up programming from various sources. That said, I have some input! Take it for what it's worth :P It's hard to define either term simply, since they describe a general approach in software design - independent of language. I think they're most typically used pertaining to the object oriented pardigm. The confusion for me is this: for both, you hide some things. But... For abstraction, you mean to simplify. You do everything you must internally, but make the external stuff minimalist. For encapsulation, you mean to isolate. Regardless of what you have, you keep it together (exposed or not)! Cars - engines, gas pedal: Abstraction: You hit the gas pedal, you go. You don't worry about the rest - you're no mechanical engineer. You hit the gas, you go. Encapsulation: All of the combustion (and all the pistons) is inside the engine. We're not having combustion in different places. When you work with the combustion, you go there. If you're coolant you go there. If you're gasoline, you go there. Abstraction makes it simple to use a car. Easy to use. Encapsulation makes it simple to manage the combustion. It wraps the combustion up in a nice package. If you're a fan of verbosity, feel free to continue.. But you did ask for "simple words," so please feel free to ignore this section; it'll be here for others if they want. ------------------------------------------------------------ Abstraction can take place at every level, but I think the most clear cut example that I consider to be abstraction deals with a low level we don't often use directly. If you're writing high level code, then you're not directly telling the computer to store like-typed data continuously in memory with the necessary element size and whatnot. This is abstracted away, because we don't care about the particular process when we work at a high level. We would use an array or list datatype for their ease of use. This is abstraction because the details of memory location, datatype size, resizing algorithm, insertion algorithm, traversing algorithm and more are all away from your sight. Encapsulation is more about keeping everything together. The Wikipedia article has an interesting take in it's intro. https://en.wikipedia.org/wiki/Encapsulation_(computer_programming) But, for my two cents, I'll keep it general. For me, encapsulation is taking everything that's used for the same exact (although maybe general) purpose and putting it all in one place. If two people are together all the time, and in their purpose they are never separated, they can be encapsulated. If two people are do similar activities, but they go they're separate ways, you can't encapsulate them. Suppose you have data and a function to act on that data. It's possible to keep them tied together as long as that data isn't needed elsewhere and that function isn't used on other data. Now, if there are two functions that use the same data, but the functions aren't used for any other data, you can encapsulate them all; but you can't encapsulate the data with just one of the two functions that need it. For clarification, the data and functions aren't shut-ins; they're used and you can call one function or the other, but they're all together. Say I have the number "1000" as data, and two functions such that one will transform it to "1,000" and the other will transform it to "1k." If I group them (like make an class around them), then the data and functions are in their own bubble. That's much cooler than having a variable and two functions separate. I might wrongly use the function on data that has no purpose being used in that function. And I might wrongly use the data for a function that isn't useful for that data. They'd be loose, free to use however. While they're encapsulated, they only work with each other and they work smooothly. ------------------------------------------------------------ Haha! I just read the whole question. I'm bad about reading - I sometimes skim. Slipping through your fingers like sand is a great way to put it! Part of the reason I put so much into this was to help myself differentiate! It seems we're not the only ones, though, if there are duplicates on Stack Overflow. :)

OpenStudy (theeric):

... You guys also mentioned information hiding... *Starts novel*

OpenStudy (javk):

LOL...thank you... It's making sense again. Hopefully, this time round, it will stick So to summarize, abstraction is keeping the exposed details to a bare minimum, only relevant details are shown, whereas encapsulation is tying all the related details into a bundle. That right?

OpenStudy (theeric):

That's what I get from it! The distinctions are there. But trying to use examples to contrast them is tough because they're so intertwined! So, to clearly see the distinction, you have to ignore some details. :) Good luck! :)

OpenStudy (theeric):

Ignore some details to separate the ideas, I mean.

OpenStudy (javk):

I know this is late and kindda silly of me to go back to, but anyways, I was just studying this...again...and these are the definitions I am dealing with. They highlight what you guys said in terms of subtle differences between the two i.e. abstraction and cohesion. so here goes... Abstraction is when the client of a module doesn't NEED to know more than is in the interface Encapsulation is when the client of a module isn't ABLE to know more than is in its interface

OpenStudy (theeric):

That's a nice summarization, thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!