Abstraction

Abstraction

Abstraction

Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.

Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.

Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.

Thus, we can say a television clearly separates its internal implementation from its external interface and you can play with its interfaces like the power button, channel changer, and volume control without having zero knowledge of its internals.

Now, if we talk in terms of C++ Programming, C++ classes provides great level of data abstraction. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e., state without actually knowing how class has been implemented internally.

For example, your program can make a call to the sort() function without knowing what algorithm the function actually uses to sort the given values. In fact, the underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, your function call will still work.

In C++, we use classes to define our own abstract data types (ADT). You can use the cout object of class ostream to stream data to standard output like this:


                         int main( )
                                {              
                          cout<<"Hello C++";
                                  return 0;
                                 }
 

Here, you don't need to understand how cout displays the text on the user's screen. You need to only know the public interface and the underlying implementation of cout is free to change.

Types of Abstraction

There are two broad types of abstraction :

  • Functional Abstraction
  • Data Abstraction.

The main difference between functional abstraction and data abstraction is that functional abstraction refers to a function that can be used without taking into account how the function is implemented.

Data abstraction refers to the data that can be used without taking into account how the data are stored. There is also a difference in the way the access takes place in functional abstraction and data abstraction.

In functional abstraction , access to the function is provided through a specific interface defined to invoke the function. In contrast, in data abstraction, access to the data is provided through a specific set of operations defined to examine and manipulate the data. For instance, when a programmer is using C++ standard data types, this means that users are using the concept of data abstraction. When using data types, the users are not concerned with how the data is stored but they are concerned with what operations are provided and what properties are supported.

Need of Abstraction

  • Flexibility in approach : By hiding data or abstracting details that are not needed for presentation, the programmer achieves greater flexibility in approach.

  • Enhanced Security : Abstraction gives access to data or details that are needed by users and hide the implementation details, giving enhanced security to application.

  • Easier Replacement : With the concept of abstraction in object-oriented programming language, it is possible to replace code without recompilation. This makes the process easier and saves time for users.

  • Modular Approach : In object-oriented programming language C++, the abstraction concept helps users to divide the project application into modules and test each of them separately. Then all modules are integrated and ultimately tested together. This approach makes the application development easier.


Copyright © 2014 . All Rights Reserved.

No comments:

Post a Comment