Alex Rivera | Logout

Difference between abstraction and encapsulation?

Asked 2009-04-12T20:15:02.410
460

What is the precise difference between encapsulation and abstraction?

Edit
Report

3 Answers

29
  • Abstraction lets you focus on what the object does instead of how it does it
  • Encapsulation means hiding the internal details or mechanics of how an object does something.

Like when you drive a car, you know what the gas pedal does but you may not know the process behind it because it is encapsulated.

Let me give an example in C#. Suppose you have an integer:

int Number = 5;
string aStrNumber = Number.ToString();

you can use a method like Number.ToString() which returns you characters representation of the number 5, and stores that in a string object. The method tells you what it does instead of how it does it.

answered 2009-04-12T20:22:35.980
4

Encapsulation is wrapping up complexity in one capsule that is class & hence Encapsulation… While abstraction is the characteristics of an object which differentiates from other object...

Abstraction can be achieved by making class abstract having one or more methods abstract. Which is nothing but the characteristic which should be implemented by the class extending it. e.g. when you inventing/designing a car you define a characteristics like car should have 4 doors, break, steering wheel etc… so anyone uses this design should include this characteristics. Implementation is not the head each of abstraction. It will just define characteristics which should be included.

Encapsulation is achieved keeping data and the behaviour in one capsule that is class & by making use of access modifiers like public, private, protected along with inheritance, aggregation or composition. So you only show only required things, that too, only to the extent you want to show. i.e. public, protected, friendly & private ka funda…… e.g. GM decides to use the abstracted design of car above. But they have various products having the same characteristics & doing almost same functionality. So they write a class which extends the above abstract class. It says how gear box should work, how break should work, how steering wheel should work. Then all the products just use this common functionality. They need not know how the gear box works or break works or steering wheal works. Indivisual product can surely have more features like a/c or auto lock etc…..

Both are powerful; but using abstraction require more skills than encapsulation and bigger applications/products can not survive with out abstraction.

answered 2011-06-01T06:39:21.920
1

I Think Encapsulation is a way to implement abstraction. Have a look at the following link.

Abstraction and Encapsulation

answered 2013-06-23T02:50:37.483

Your Answer