Sandy
Sandy

Reputation: 1

confusion about abstraction and encapsulation

Can anyone tell me with examples abstraction and encapsulation and difference between them?

Upvotes: 0

Views: 498

Answers (2)

dkamins
dkamins

Reputation: 21918

Encapsulation:

Encapsulation is essentially the hiding of implementation details, separating data and behavior, and providing simple interfaces to complex black box entities.

Example: you press the toast lever down on your toaster, it activates the heating coils & timer, and your toast pops up ready to eat in a minute. The details of making toast are encapsulated into a simple interface of pushing the lever down. In 20 years, you might buy a nuclear toaster, but the same encapsulation of functionality will allow you to use it in the manner you're accustomed to now.

Abstraction:

Definition 1

In the general sense, the interface provided by an encapsulation is essentially an abstraction for its more complex innards. With this definition, the distinction is blurred.

Example: the same toaster from above can be said to be abstracting the complex action of toasting bread into a simple operation of pushing a lever.

Definition 2

Another more distinct meaning is the use of a hierarchy of types or other generality to allow entities to be used interchangeably in their more generic forms. Encapsulation often enables this type of abstraction.

Example: your toaster can toast slices of wheat bread, rye bread, raising bread, or even bagels. The only requirement is that they be sliced and limited to some common set of dimensions. Here we might say that your toaster can toast anything "Toastable". This is an abstraction that encompasses all of these items.

Grady Booch says...

(from http://en.wikipedia.org/wiki/Information_hiding)

In his influential book on object-oriented design, Grady Booch defined encapsulation as "the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation."[2] This formulation is cited by a number of books as an authoritative definition of encapsulation.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881403

They're certainly related, but subtly different.

Abstraction is the treating of concrete classes (types of objects) as abstractions. By that, I mean you don't have classes representing square, circle, rectangle and so on. All these can be abstracted to a common ancestor, shape. The abstraction is useful because there are certain operations that you can perform on any shape, such as drawing, setting the origin, setting the size and so on.

Encapsulation, on the other hand, is information hiding. It's the act of exposing only enough information to enable you to do what needs to be done.

A classic example there is a telephone book. You don't expose the fact that you're using a hash table or a couple of arrays or whatever bizarre structure you've chosen to represent it. Instead you provide only the operations that you expect to carry out on an object.

Encapsulation lets you change the internals of a class without having to worry about effects on the client. So maybe an array is fine when your phone book has twenty entries in it but, when you get so popular that people are clamouring to be added, you may want to switch to using hash tables or a database.

With encapsulation, this is a far more painless process since, as long as you maintain the public face of your class, you can change what you want. If you had "published" your internals, people may have come to depend on that, to the point where that dependency would break their code when you changed it. A good rule is to only "publish" what you expect to never change.

Encapsulation eases the task of abstraction since the internal details of an class are hidden from view.

Upvotes: 3

Related Questions