Friday, July 29, 2011

Encapsulation vs abstractions

“Abstraction and encapsulation are not synomous,” says P.J. Plauger in Programming on Purpose.



Encapsulation is information hiding. In other words: "Information hiding allows for implementation of a feature to change without affecting other parts that are depending on it.”
Encapsulation is a process where you group all data and methods (way to process data) together under an umbrella. This is a kind of normalization that we do on the object and its behavior.
Also, Encapsulation can be defined as the procedure of packing data and operations that operate on them into a single entity. This means that to access data, certain predetermined methods should be used. In other words, the data contained are not directly accessible. This ensures that data integrity is preserved because the user is unable to directly approach and modify the data as he / she wants. Users will receive or will the data values ​​only by methods that are publicly available to users.


Abstraction: Look at it as “what” the method or a module does not “how” it does it.
Definition: “The notion abstraction is to distill a complicated system down to its most fundamental parts and describe these parts in a simple, precise language.”
Abstraction is the way to hide complex implementation from outer world. For example: List.Sort() will sort the list and will give back the sorted list. User need not to worry about what algorithm the Sort() function has used. Object oriented programming/language offers this kind of abstraction.
Abstraction is the process of separating the details of presentation of the implementation details. This is done so that the developer is relieved of the complex details about implementation. The programmer can instead concentrate on the presentation or details of behavior of the entity. In simple terms, the abstraction focuses how a certain entity can be used rather than how it is executed. The abstraction hides the implementation details essentially, that even if the implementation methodology completely changes the time, the programmer should not worry how it would affect his program.

·         Encapsulation protects abstraction.
·         Encapsulation is the bodyguard, Abstraction is the VIP.
S.No.
Abstraction
Encapsulation
1
VIP
Assistant
2
Technically abstraction is like use of methods in a class by creating an object of that class, and u no need to know how the these methods are defined and worked
Encapsulation keeps code and data, it manipulates, safe from the outside code, i.e. it work like a wrapper for the inside code.
3
Abstraction is showing only essential details.
Encapsulation is hiding the working of abstraction in a class or template
4
Example:
A real world example, consider u have setup a big building(say a company), the details regarding materials used to build (glass, bricks), type of work, manager of the company, number of floors, design of the building, cost of the building etc. can be classified as ABSTRACTION.
Whereas, type of glass or bricks (grey one or red one) used, who all work for which all departments n how they work, cost of each and every element in the building etc. comes under Data ENCAPSULATION.
5
Outer layout, used in terms of design.
Inner layout, used in terms of implementation.
6
Abstraction provides business value; encapsulation "protects" these abstractions. If a developer provides a good abstraction, users won't be tempted to peek at the object's internal mechanisms. Encapsulation is simply a safety feature.
Encapsulation provides the explicit boundary between an object's abstract interface (its abstraction) and its internal implementation details.
7
In order to process something from the real world we have to extract the essential characteristics of that object.

Data abstraction can be viewed as the process of refining away the unimportant details of an object, so that only the useful characteristics that define it remain. Evidently, this is task specific.
Encapsulation is one step beyond abstraction. Whilst abstraction involves reducing a real world entity to its essential defining characteristics, encapsulation extends this idea by also modeling and linking the functionality of that entity. Encapsulation links the data to the operations that can be performed upon the data.

Abstraction has the methods which will be common for all the derived class would need. It contains the skeleton which needs to be implemented by the derived class also, which will be declared as abstract method.

Example:

abstract class CurryBase
{
public abstract void doCurryMasala();
}

public class FishCurry : CurryBase
{
public void doCurryMasala()
{
// Add Fish Curry specific contents also.
}
}
Encapsulation is basically, wrapping up of data members and methods.
As you said, You hide the data for security such as making the variables as private, and expose the property to access the private data which would be public. So, when you access the property you can validate the data and set it.

Example:

Class Demo
{
private int _mark;

public int Mark
{
get { return _mark; }
set { if (_mark > 0) _mark = value; else _mark = 0; }
}
}

Have a nice day ahead.

No comments:

Post a Comment