Monday, December 21, 2009

Difference between encapsulation and abstraction

Encapsulation:
----------------------
Encapsualation is basically, wrapping up of data memebers 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; }
}
}

Abstraction:-
------------------
Abstraction have the methods which will be common for all the derived class would need. It contains the skeletion 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 the concept of binding data with methods...

Abstraction is nothing but hiding of data...in the sense hiding of complexity and providing functionallity..

No comments:

Post a Comment