What does "data abstraction" exactly mean?

Naruto picture Naruto · Sep 3, 2010 · Viewed 43.4k times · Source

What does data abstraction refer to? Please provide real life examples alongwith.

Answer

aaronasterling picture aaronasterling · Sep 3, 2010

Abstraction has two parts:

  • Hide details that don't matter from a certain point of view
  • Identify details that do matter from a certain point of view and consider items to be of the the same class if they possess those details.

For example, if I am designing a program to deal with inventory, I would like to be able to find out how many items of a certain type the system has in stock. From the perspective of the interface system, I don't care if I am getting this information from a database, a csv file, a remote repository via a SOAP interface or punch cards. I just care that I can can say widget.get_items_in_stock() and know that it will return an integer.

If I later decide that I want to record that number in some other way, the person designing the interface doesn't need to know, care or worry about it as long as widget still has the get_items_in_stock() method. Like wise, the interface doesn't need to care if I subclass the widget class and add a get_square_root_of_items_in_stock() method. I can pass an instance of the new class to it just as well.

So in this example, we've hidden the details of how the data is acquired and decided that anything with a get_items_in_stock() method is an instance of the same class (or a subclass thereof) for certain purposes.