Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Try this link - http://www.itmweb.com/essay550.

htm

Encapsulation is nothing but showing only those things which are required by
wrapping up complexity & hence it is also called that it leads to data hiding but its
not data hiding. Never say its data hiding… so never say its data hiding, its
wrapping up complexity in one capsule that is class & hence Encapsulation…
While abstraction denotes the essential characteristics of an object which
differentiates from other kinds of object...

Abstraction is achieved by making class abstract having one or more methods


abstract. Which is nothing but essential 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.

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.
}
}

You might also like