3

When studying OOP, mainly java, these two concepts are always linked, but I see no real relationship or similarity between them. Correct my if I'm wrong but:

Abstraction is to identify key parts of an object and ignore everything else, used in interfaces. Encapsulation just describes how data is protected and hided, it affects permissions.

I don't see a real relation between the two, is there one? Also a simple description for each if mine were bad in any way would help.

Arroza
  • 33
  • 4
  • 2
    Possible duplicate: [Abstraction VS Information Hiding VS Encapsulation](http://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation) – Devendra D. Chavan Apr 12 '12 at 02:16
  • Those answers helped me understand more about Abstraction and Encapsulation but I don't see a relationship between the two. – Arroza Apr 12 '12 at 02:20
  • 1
    @Arroza The accepted answer starts with "Abstraction and encapsulation are complementary concepts..." and goes on to explain how, with an example. – Paul Bellora Apr 12 '12 at 02:23
  • possible duplicate of [precise difference between encapsulation and abstraction](http://stackoverflow.com/questions/742341/difference-between-abstraction-and-encapsulation/13589763#13589763) – NoNaMe Nov 28 '12 at 04:26

3 Answers3

2

Those two concepts serve the same goal, in a way, even that Abstraction is more about the design, and encapsulation is more about the implementation (there are no fields and data when discussing Abstraction).

This common goal is to give the user a simple and well defined interface to work with, so he doesn't need to bother about the details, but only about what it does and how he connects with it.

As an example, if you look at on/off switch, Abstraction will define it's two conditions, and the way to switch between them, and encapsulation will choose to hide the wires and stuff inside the switch, and will let you see the familiar switch we all know. The common is that it's usage is simple and standard, thanks to these two concepts.

There is more to say about each of these concepts, but this is what I can tell you about the relation between them.

Imran
  • 1,665
  • 2
  • 18
  • 40
Lior
  • 151
  • 4
0

Scott Ambler has a nice resume of the topic:

Instead of saying we determined what a class knows a does, we say we "abstracted" the class. Instead of saying we designed how the class will accomplish this things, we say "encapsulated" them (...)

The author uses the example of a college student for a enrollment system. Of the many attributes that a student has (hair color, religion, ethnicity) and the many responsibilities that he would have (partying, playing sports, go to library); we choose that we need to store only the student name and address, and that every student should be able of enrolling in seminars. That's our abstraction of student.

But we still doesn't know how our system will perform the enrollments. A typical Java application is composed of classes, and this classes has attributes and methods. We say then that the system functionality is encapsulated in classes, and that each class functionality is encapsulated in methods. Encapsulation tells how the functionality will be implemented but hides implementation details.

Carlos Gavidia-Calderon
  • 6,905
  • 8
  • 30
  • 58
0

Abstraction Abstraction focuses on the outside view of an object. shows only what is necessary.

Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.

Rohit
  • 253
  • 2
  • 3