Click here to Skip to main content
14,739,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Looks like I have lost my basics but really confused between the two. What is the core of the matter that differentiates the Encapsulation from Abstraction?

As far as I get it:
Encapsulation is binding of function, method, properties, events etc. in one capsule or Class with appropriate level of abstraction (access modifiers)

and

Abstraction is hiding of data using access modifier.

This is a kiddish definition. I want a fully qualified professional difference between the two.

Please help me to get my ground 0 right :doh:
Posted
Comments
Sandeep Mewara 14-Feb-11 1:45am
   
Update from Enquirer:
My friend says:

Encapsulation is a concept (which means hiding)
and it has two branches by which it is implemented:
1) Abstraction (abstract classes)
2) Interfaces

Is this correct??

Hello)
I will try to show a point of view.
u see, objects in real world usually have millions of properties and methods.but it does not mean that u will use all of them in your programm.Oppositly,u use only those that u need accordin to the context of your programm.for example,if u write application that calculates the salary,u will not need such properties of human like height of weight,and vise versa,writing apllication for medical clinic,u will definetly need those parameters.So,in few words, abstraction is when u decide which properties u will use in particular programm,and which u wont.

now about encapsulation.
as it can be guessed from the name,its just a hiding the reslisation from user(capsule).what for - to make the user unable to affect the process.for example - for car driwer starting the engine is just a matter of spinning key.but for manufacturer it is rather complicated procedure - firstly it joins contacts,then turning on starter,then candle generates spark - and finally engine is working.incapsulation makes sure that driwer wont interrupt that complicated process(cuz there is a chanse that otherwise engine will be broken).In terms of programming - if u want,for example,validate the email-adress,u should encapsulate private variables that are used within that validation procedure(to prevent modifiring them and getting wrong answer ()).
   
Comments
Amit Kumar Tiwari 14-Feb-11 3:42am
   
Good explaination :)
Dalek Dave 14-Feb-11 3:52am
   
If you like, I can fix the Txtspk, Spelling, Grammar, Syntax and Readability Issues.
Then work on the definition of the answer so that many others may understand it.
Thank you for your question.

Encapsulation is hiding implementation from end user. Example:

String str1="Hello";
Console.Write(str1.Length);


In the above example, str1 String instance object hide implementation details of Length from client.

Abstraction is everything that need to build an object. In real world consideration, to build a car you need wheels, color, steering wheels, looking glass etc. All things are needed to abstraction a car object.

Example:
public class Car
{
   int _wheel;
   String _color;
   int _glass;

   public int Wheel
   {
     get {return _wheel;}
     set { _wheel=value;}
   }

  public String Color
  {
     get {return _color;}
     set { _wheel=color;}
  }

  public int Glass
  {
     get {return _glass;}
     set { _glass=value;}
  }
}


Has this example made you clear?


Thanks,
Mamun
   
v6
Comments
Amit Kumar Tiwari 14-Feb-11 1:29am
   
Encapsulation clear now :), abstraction is not clear yet :(
Amit Kumar Tiwari 14-Feb-11 1:49am
   
Thank you so much for the examples. So if I understand clearly, encapsulation is hiding the implementation of function or method while Abstraction is grouping of class members with access modifiers. Am I right???
Abdul Quader Mamun 14-Feb-11 1:52am
   
Not function or method only fields.
Amit Kumar Tiwari 14-Feb-11 1:53am
   
Great, thank you so much Mamun for sharing your knowledge with me :) Allah bless you.
Amit Kumar Tiwari 14-Feb-11 2:04am
   
One last question, why do some website confuse by saying encapsulation means Binding data and member functions together inside a single unit.

example - http://www.dotnetspider.com/resources/18616-OOPS-Concepts-Net-Shake-your.aspx
Abdul Quader Mamun 14-Feb-11 2:40am
   
They were not wrong. They described using their language. Binding meant hind implementation.
Abstraction:

Abstraction is a process of hiding the implementation details and displaying the essential features.

Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works. You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.

So here the Laptop is an object that is designed to hide its complexity.
How to abstract: - By using Access Specifiers

.Net has five access Specifiers

Public -- Accessible outside the class through object reference.

Private -- Accessible inside the class only through member functions.

Protected -- Just like private but Accessible in derived classes also through member
functions.

Internal -- Visible inside the assembly. Accessible through objects.

Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.


Encapsulation:-

Encapsulation is a process of binding the data members and member functions into a single unit.

Example for encapsulation is class. A class can contain data structures and methods.
Consider the following class

public class Aperture
{
public Aperture ()
{
}
protected double height;
protected double width;
protected double thickness;
public double get volume()
{
Double volume=height * width * thickness;
if (volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900