Professional Documents
Culture Documents
Abstraction and Encapsulation
Abstraction and Encapsulation
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...
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;
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: