OCR H446, section 2.1.1 (a)
Computational thinking is a way of approaching problems so that a computer (or a person) can solve them. Abstraction is one of its central ideas.
What is abstraction?
Abstraction means picking out the important features of a problem and ignoring the details that don't matter. It makes a complicated situation simpler to think about, so that it can be represented in a program.
You use abstraction all the time:
- A map of the London Underground shows the stations, lines and connections, and leaves out the real distances, the bends in the track and the streets above. All a passenger needs is how to get from A to B.
- A variable name such as
scorestands for a memory location. You don't think about the binary address. - When you drive a car, you use the steering wheel and pedals. You don't need to think about how the engine works.
Forms of abstraction
Removing detail (representational abstraction)
A model keeps only the details that are needed for a purpose. The same real thing can have different abstractions for different purposes.
| Real thing | Purpose | What the model keeps | What it leaves out |
|---|---|---|---|
| A town | Finding the shortest route | Junctions and roads with distances (a graph) | Buildings, scenery, traffic lights |
| A student | A school register | Name, form, attendance | Hobbies, height, hair colour |
| A chess game | A chess program | Squares and the piece on each | The look of the pieces |
| A bank account | Online banking | Account number, balance, transactions | The owner's life story |
Generalisation
Abstraction by generalisation means finding what several things have in common, and treating them as one kind of thing. A Dog, a Cat and a Horse are all Animals, which have a name, an age and a speak() method. This is the idea behind classes and inheritance.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal): # a Dog IS A KIND OF Animal
pass
Data abstraction
Hiding how data is stored, and showing only what can be done with it. A stack is used with push and pop, and the user doesn't need to know whether it is an array or a linked list.
Procedural (functional) abstraction
Hiding how a task is done, and showing only what it does. When you call sorted(numbers), you don't need to know whether it uses merge sort or another method.
Layers of abstraction
Computing is built from layers, each one hiding the details of the layer below:
- Each layer provides a simple interface to the one above.
- A person working at one level needn't understand the levels below.
- The layers can be changed independently: a new disk can be fitted without changing the word processor.
The TCP/IP stack (1.3.3 (b)) and the operating system (1.2.1) are examples of layered abstraction.
Abstraction in programming
| Technique | How it abstracts |
|---|---|
| Variables and constants | Names stand for values and memory locations |
| Data types and structures | Hide how numbers, text and collections are stored |
| Subroutines (functions and procedures) | Hide the steps behind a name and a set of parameters |
| Classes and objects | Combine data and behaviour, and hide the inside (encapsulation) |
| Libraries and APIs | Offer ready-made features without showing the code |
| Models (graphs, state machines) | Represent real situations in a form that can be processed |
Worked example
Task: an app tells people the quickest way to get between two bus stops in a city. What details would be kept in the abstraction, and what would be removed?
- Kept: the bus stops (nodes), the routes between them (edges), the journey time on each link (weights), and the times buses run.
- Removed: the colour of the buses, the names of the drivers, the shape of the roads, buildings along the way, and the weather.
- Result: the city becomes a weighted graph, which a program can search for the quickest route (for example with Dijkstra's algorithm, 2.3.1).
The important point is that what you keep depends on the problem. For a program that shows tourists the scenery, a very different abstraction would be needed.
Exam tip: define abstraction as removing unnecessary detail to focus on what matters, and give an example in context that says what is kept and what is left out. Mention that abstraction can be applied in layers.
Check yourself
What is abstraction?
Focusing on the important features of a problem and ignoring unnecessary detail, so that it can be represented in a simpler way.
Why is the London Underground map a good example of abstraction?
It keeps the stations, lines and connections that passengers need, and leaves out real distances, the bends in the track, and other detail.
Give two examples of abstraction in programming
Any two of: using a subroutine without knowing how it works; variable names for memory locations; a class that hides its data; a library or API; a stack used through push and pop.
What is a layer of abstraction?
A level that provides a simple interface to the level above, hiding the details of how the levels below work.