The difference between the concepts of encapsulation and abstraction

Possible duplicate:
VS abstraction. Information hiding VS encapsulation.

Can someone explain to me the main differences between the principles of encapsulation and abstraction in object-oriented programming (if possible with examples).

+6
source share
4 answers

Example:

// NO ABSTRACTION, NO ENCAPSULATION const int catLegs = 4; const int spiderLegs = 8; Leg[] catLegs; Leg[] spiderLegs; void MakeCatRun(Distance b) { for (int i=0; i<catLegs; ++i) catLegs[i] += b; } void MakeSpiderRun(Distance b) { for (int i=0; i<spiderLegs; ++i) spiderLegs[i] += b; } 

Encapsulation:

 // ENCAPSULATION class Cat { Leg[] legs; int nLegs; public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; } } class Spider { Leg[] legs; int nLegs; public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; } } 

Abstract:

  // ABSTRACTION class LivingBeing { Leg[] legs; int nLegs; public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; } } class Cat: LivingBeing { } class Spider: LivingBeing { } 
+6
source

Abstraction is a quality that we simply don’t care about unnecessary internal mechanisms (implementations) and can deal with the system / object, considering the essence.

For example: When using brakes on a car, you don't care if it has an air brake or a hydraulic brake. Abstraction occurs in the form of pressing the pedal.

Encapsulation is what makes the above (abstraction) possible by packing (encapsulating) implementation details in a container (hiding the mechanisms of disturbance and tiny components from your vision or physical contact, in the above case).

So encapsulation actually provides an abstraction!

If you look around you can see it everywhere around you in the real world. In addition, he is present in programming. If someone provides you with a class for sorting integers, you don’t have to worry about the sorting algorithm (bubble sort / Quick sort) that it uses. Abstraction allows you to go through a list of integers to a method; i.e.

  class Sorter { public List<Integer> Sort(List<Integer>)//Only this method is seen outside { String pattrenName=this.AdvancedPatternFinder(); this.Advancedsorter(pattenName); //Return sorted list } private String AdvancedPatternFinder(){}//NOT seen from outside private void Advancedsorter(String pattrenName){}//NOT seen from outside } 

See the animation below to see how neat abstraction is provided by encapsulating internal parts inside!

image courtesy: this blog

enter image description here

+5
source

Encapsulation means that the internal state (data) of an object can be changed only by its public methods (open interface):

 class Encapsulated { private int data; public int getData() { return data; } public void setData(int d) { data = d; } } 

Abstraction means that you can abstract from specific implementations, for example:

 abstract class Container { int getSize(); } class LinkedList extends Container { int getSize() { /* return the size */ } } class Vector extends Container { int getSize() { /* ... */ } } 

If you use the abstract class Container in all your code instead of Vector or LinkedList, you can switch the specific implementation of Container (Vector / LinkedList in this case) without changing any code, thereby abstracting yourself from the implementation.

+4
source

The two concepts are different, but closely related and often found together.

Abstraction hides non-essential details, usually called implementation details. For example, to read data from a stream, most environments have an InputStream concept. It provides a ReadBytes () method to extract more bytes from a stream. How this happens, it is abstracted - it can be reading from a file, using the OS APIs or reading from a socket, from SSH or something else that can be represented as a stream of bytes. The InputStream class is called an abstraction.

Each implementation of InputStream encodes how bytes are read. For example, FileInputStream reads bytes from a file and maintains the current offset to the file as a private data item. Code that is open to the class does not have free access to read or write this variable directly - this can lead to incorrect results, such as part of a skipped stream or repeated reading. This is especially important in multi-threaded scenarios when changes to a variable must be carefully monitored. Instead of providing unlimited access, the class uses offset internally (declaring it private ) and provides external code only indirect access, for example, using the GetCurrentOffset() method and the Seek(int offset) methods. Then the offset variable is encapsulated by the FileInputStream class.

+1
source

All Articles