3

Possible Duplicate:
Abstraction VS Information Hiding VS Encapsulation

The definitions of "abstraction" and "encapsulation" seem very similar to me. I always confuse these terms. Please, clarify the difference by showing examples.

Community
  • 1
  • 1
Satya
  • 7,542
  • 9
  • 35
  • 39
  • which definitions seem to be similar? give the definitions you don't understand, so that we clarify. Otherwise - read wikipedia? – Bozho Dec 18 '09 at 08:23
  • oops sorry, just changed it :) – skaffman Dec 18 '09 at 08:24
  • 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:25

5 Answers5

13

In this forum post a short but clear (I think) answer is provided:

Encapsulation has two faces; data abstraction and information hiding. Data abstraction is a type seen from the outside. Information hiding is a type seen from the inside.

  • Abstraction focuses on the outside view of an object (i.e. the interface)
  • Encapsulation (information hiding ) prevents clients from seeing its inside view, where the behavior of the abstraction is implemented
Konamiman
  • 47,560
  • 16
  • 107
  • 133
12

Abstraction is about focusing on some key features of objects, and deliberately ignoring the other features. For example, what do the following have in common:

Plane
Train
Automobile

Right, they all have wheels! And our object model has a method.

getNumberOfWheels()

We have abstracted the common feature that we care about for this particular problem. Sure, you probably were expecting some other abstraction focusing on numberOfPassengers() or speed(), but the idea is that we are working in some particualr problem area, and so focus on some particular abstraction.

In a more realistic example we might have Janitor, Senior Executive, Sales Person, Manager and so on. And our abstraction focuses on their payment. So we have

 getSalary()

Now we see something interesting. For the Janitor the salary is computed by

 hoursWorked * paymentPerHour

For the sales person it's

 baseSalary + commision

but when we look at the abstraction getSalary() we don't see those details, just the capability of answering the question "what is the salary"; we have Encapsulated the salary computation.

Abstraction: Identifying the relevent essential feature seen from the outside

Encapsualtion: Hiding the details of how those features are implemeneted

djna
  • 52,574
  • 11
  • 70
  • 109
2

They are actually related.

Abstraction refers to the high level view of a object or concept. For example a Car is an abstraction which gives a high level view without referring to the details (such as the door, steering wheel, seats, etc). Base classes are normally abstractions which separate common attributes.

Encapsulation goes one step further and is the hiding of the implementation details of the abstraction. For example, you know that a Car has a door, steering wheel, seats, etc but you are forbidden from knowing the details of how the these elements are implemented (these details are encapsulated).

Yukiko
  • 1,083
  • 1
  • 8
  • 10
0

Abstraction It is a class the defines the behavior of classes that extend it. nothing more nothing less.

Encapsulate It is something that defines which methods and properties of class are accessible or not.

Conclusion You have made the wrong comparison, they are totally different.

However, mostly people get confused between an interface and the abstract class, this seems to be little logical question, but then again not that correct one.

Sarfraz
  • 355,543
  • 70
  • 511
  • 562
0

They're quite distinct, actually...

Abstraction: The idea of presenting something in a simplified / different way, which is either easier to understand and use or more pertinent to the situation.

Consider a class that sends an email... it uses abstraction to show itself to you as some kind of messenger boy, so you can call emailSender.send(mail, recipient). What it actually does - chooses POP3 / SMTP, calling servers, MIME translation, etc, is abstracted away. You only see your messenger boy.

Encapsulation: The idea of securing and hiding data and methods that are private to an object. It deals more with making something independent and foolproof.

Take me, for instance. I encapsulate my heart rate from the rest of the world. Because I don't want anyone else changing that variable, and I don't need anyone else to set it in order for me to function. Its vitally important to me, but you don't need to know what it is, and you probably don't care anyway.

Look around you'll find that almost everything you touch is an example of both abstraction and encapsulation. Your phone, for instance presents to you the abstraction of being able to take what you say and say it to someone else - covering up GSM, processor architecture, radio frequencies, and a million other things you don't understand or care to. It also encapsulates certain data from you, like serial numbers, ID numbers, frequencies, etc.

It all makes the world a nicer place to live in :D

Sudhir Jonathan
  • 15,629
  • 12
  • 60
  • 85