What Encapsulation and Abstraction?
1 Encapsulation:
Encapsulation refers to the process of hiding or
encapsulating the implementation details of an object. A washing machine is a good example
of an encapsulated object.
We know that inside a washing machine are a whole series
of complex electronics, however, we don ít need to be able to understand them to
wash our clothes. In fact if the nitty gritty electronics were exposed to us we might
even be afraid to use it and the washing machine would be more fragile and in danger
of breaking down.
This was one of the disadvantages of procedural programming;
the user of the procedure
could break the procedure by giving it the wrong type of
data.
In terms of our concept of an object, encapsulation hides
the properties, some methods, all method implementation details of an object
from the outside. For example, the velocity of a car cannot be magically
changed, we have to press the accelerator or brake (methods that we don't need to know the details
of), in this respect the velocity of the car is hidden from outside
interference, but can be changed by visible
methods.
An interface is a simple control panel that enables us to
use an object. In the case of a washing machine the interface consists of
the powder drawer, the door, the program knob and the on/o§ switch. For a car we have
the steering wheels, clutch, brake accelerator etc. The great benefit of the
interface is that we only need to understand the simple interface to use the washing
machine or car in order to use them. This is much easier than understanding the internal
implementation details.
Another benefit is that the implementation details can be
changed and we can
still use the car or washing machine. For example,
Suppose your car breaks down and
you take it to the garage and they replace the engine
with a bigger and better engine.
The car operates in exactly the same way as the interface
has remained constant.
Thus it is extremely important to design a good interface
that will not change. The
inner workings can be tinkered with and cause no external
operational aspect.
Taken together the encapsulation and interface concepts
unite to produce another benefit. Once the user understands the interface, the
implementation details.
2 Data Abstraction.
Abstraction is a general concept in computer science and
means disregarding the
details of an implementation and focusing instead on the
ideal being represented.
For example, consider what happens when you click on the
print button in a word
processing application. It is possible to simply imagine
some process which moves the
contents of the document to the printer, which then
prints the document. It would
be complex and confusing to think about the actual
software being executed on your
computer, the network server and the printer in order to
perform printing. Instead,
an abstraction of the printing process is imagined.
Data abstraction is the process of creating an object
whose implementation
details are hidden (i.e. encapsulated), however the
object is used through a well defined interface. Data abstraction leads to an
abstract data type (ADT).
ADT is are objects whose implementation is encapsulated. For
example, when you use a floating
point number in a program you don ít really care exactly
how it is represented inside
the computer, provided it behaves in a known manner. ADT is should be able to be used independent of their
implementation meaning that even if the implementation changes the ADT can be
used without modification.
Most people would be unhappy if they took their car to
the garage for a service and
afterwards the mechanic said "She is running lovely
now, but you will have to use the
pedals in reverse". If this were the case the car
would not be an ADT. However, the
reality is that we can take a car to a garage for a major
overhaul (which represents
a change of implementation) and still drive it in exactly
the same way afterwards.
Thanks for visiting our site and please like our Facebook page. page: https://www.facebook.com/CodingForumBlog?ref=br_tf
Difference between Encapsulation and Abstraction
ReplyDeleteThanks for this post