AQA 7517, section 4.4.1.7
Data abstraction is a methodology that lets us isolate how a compound data object is used from the details of how it is constructed. The details of how the data are actually represented are hidden, which allows new kinds of data objects to be built from previously defined data objects.
The idea
When you use a stack, you think in terms of push, pop, peek and is_empty. You don't think about whether the items are in an array with a pointer, or in a linked list.
The stack is a data abstraction:
- How it is used (the operations): this is what the user sees.
- How it is constructed (the representation): this is hidden.
For example, a stack could be implemented as an array and a pointer for the top of the stack.
class Stack:
def __init__(self):
self._items = [None] * 10 # hidden: an array
self._top = -1 # hidden: a pointer
def push(self, item):
self._top += 1
self._items[self._top] = item
def pop(self):
item = self._items[self._top]
self._top -= 1
return item
s = Stack()
s.push(4)
s.push(7)
print(s.pop()) # 7
The code that uses s needs only push and pop. If Stack were rewritten to use a linked list, that code would not need to change.
Building new data from old
Data abstraction allows new kinds of data objects to be constructed from previously defined ones.
- A record for a student is built from strings and integers.
- A queue can be built on top of an array.
- A graph can be built from a dictionary of lists.
- A tree can be built from nodes that point to other nodes.
Each new data type is then used through its own operations, and not by reaching into the parts it was built from.
How does this differ from other abstractions?
| Abstraction | What it hides |
|---|---|
| Procedural | The actual values in a computation |
| Functional | The method of computation |
| Data | How data is represented and stored |
Benefits
- The representation can be changed for speed or memory reasons without affecting the code that uses it.
- Programmers can work with meaningful operations (such as
push) rather than raw storage. - It supports information hiding and keeps programs easier to understand and maintain.
Worked example
Task: explain how data abstraction applies to a queue of print jobs.
A print queue is used through enqueue(job), dequeue() and is_empty(). The programmer who writes the printing code doesn't need to know whether the queue is a circular array, a linked list or a Python list. If the queue is first built as a simple list, and later changed to a circular array because it is more efficient, none of the printing code changes, because it only used the three operations.
Exam tip: give a specific example and say what is hidden. "A stack is used through push and pop, while whether it is built from an array and a pointer, or a linked list, is hidden from the user" gets the marks.
Check yourself
What does data abstraction hide?
The details of how data is actually represented and constructed.
Give an example of how a stack could be implemented
As an array and a pointer to the top of the stack.
Why is it useful that the data representation is hidden?
The representation can be changed without affecting the code that uses the data, and users only have to think about the operations, not the storage.