Abstraction
You can drive a car without knowing how the engine works. That idea has a name — and computing is built on it.
Swipe or tap Next for the next section
The Steering Wheel
To drive a car, you need a steering wheel, two pedals, and a few buttons. You don't need to know how fuel burns inside the engine, how the gears mesh, or how the brakes squeeze the wheels. All of that is still happening — you just don't have to think about it.
The car's designers hid the details behind a few simple controls. That's what abstraction means: showing only what you need, and hiding the rest.
Abstraction = hiding the complicated details, so you can focus on what you're trying to do.
You Already Use It Everywhere
- A TV remote changes the channel without you knowing how the signal is sent.
- A light switch turns on a lamp without you thinking about the wiring in the wall.
- A microwave has a "Popcorn" button so you don't have to work out the timing yourself.
Each of these gives you a simple control on top of something much more complicated. The complicated part still matters — someone had to build it — but you get to use it without carrying all of that around in your head.
Layers on Layers
Computers stack abstractions on top of each other. Here's what happens when you send a two-letter message. Each layer only has to deal with the one just below it:
- You:Tap “Send” on a message that says Hi
- The app:Turns your message into text characters
- Text encoding:H is 72, i is 105
- Binary:01001000 01101001
- Hardware:Tiny switches: on, off, off, on…
You only ever see the top row. The app never thinks about switches. The hardware has no idea what "Hi" means. Every layer can do its job without knowing the details of the others.
Every page on this site has been peeling back abstractions. Underneath it all, a computer only has 0s and 1s — but almost nobody works with them directly:
- Letters are stored as numbers, which are stored as binary — see How Computers Store Text.
- Hexadecimal is a shorter way to write long binary numbers — see Binary ↔ Hex ↔ Decimal.
- An address like 192.168.1.1 is really 32 bits in a friendlier form — see IPv4 vs IPv6.
Each one is an abstraction: a simpler way to see the same binary underneath.
Abstraction in Code
Programmers use abstraction constantly. The two most common kinds are:
- Procedures (also called functions): you give a set of steps a name, then use the name instead of repeating the steps. Once drawSquare() works, you can use it without re-reading how it draws each side. Give it a parameter — drawSquare(size) — and the same steps can draw a square of any size.
- Lists: instead of making 30 separate variables for 30 test scores, you keep them all in one list called scores. One name stands in for many values.
PROCEDURE drawSquare()
{
REPEAT 4 TIMES
{
MOVE_FORWARD()
ROTATE_RIGHT()
}
}
drawSquare()
drawSquare()The last two lines say "draw a square" twice. Nobody reading them needs to think about the four moves and four turns inside — the procedure's name tells you what it does.
Why It Matters
Modern software is enormous — far too big for any one person to hold in their head. Abstraction is how people build it anyway: each piece is made simple to use from the outside, so the next person can build on it without understanding every detail inside.
It also makes changes safer. If a car company improves its engine, you still drive with the same steering wheel. If a programmer improves how a procedure works inside, every place that uses it gets the improvement — without changing a single other line.
5 quick questions. Pick one answer for each — you'll see why straight away.
A music app has a Play button. You press it without knowing how the song file is turned into sound. This is an example of…
The button hides the complicated details and gives you one simple control — that's abstraction.
A programmer writes a procedure called drawStar() and uses it ten times in a program. What is the main benefit?
Naming a set of steps lets you use it again and again without repeating or re-reading the details. Fix it once, and every use gets the fix.
Instead of 30 separate variables for 30 test scores, a student keeps them in one list called scores. Why is this an abstraction?
A list is data abstraction: the program works with "scores" as one thing instead of juggling 30 separate names.
The code inside a procedure called calculateTax(price) is rewritten to run faster. It still takes a price and returns the tax. What else in the program has to change?
That's the payoff of abstraction: as long as a procedure's inputs and results stay the same, the details inside can change freely.
A procedure drawSquare(size) takes a parameter. Why is that more useful than a procedure that always draws a square with sides of 10?
A parameter makes a procedure general: the same steps work for many different values, so you don't need drawSmallSquare, drawBigSquare, and so on.