Skip to content
Skip to main content

About this free course

Download this course

Share this free course

Introduction to computational thinking
Introduction to computational thinking

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

2.3 Encapsulation in computing

Abstraction as encapsulation is common everywhere in computing. Sometimes it is referred to as ‘abstraction as information hiding’ (instead of encapsulation).

Let us consider an example. Our example will make use of the Python programming language. This language is used throughout M269 Algorithms, data structures and computability on which this OpenLearn course is based. It was chosen because it is eminently suitable for learning the basic concepts of computer science and programming. It allows you to write concise, easily understood programs. You should be able to follow most of the following discussion if you have had some experience with an imperative programming language.

The incredibly short Python program below will display the date and current time (see Figure 13). It depends on a built-in Python module which is imported in the first line.

import time


Figure 13 Execution of the small program above in the Python Interactive Shell

How does time.ctime() work? We don’t know and we don’t need to know! Someone has written the time module and documented its interface, which contains the function ctime(). We can just use the function without any knowledge whatsoever of its implementation.

However, under the bonnet the time module is itself a program, written in C, another high-level programming language! The programmers of the module, when they wrote it, only needed to know C. They were able to ignore the details of the low-level machine language that the C program would get translated into.

So now we have three layers of programming abstraction (Figure 14).

Described image
Figure 14 Three layers of abstraction as encapsulation

Think about it: if layers of encapsulation did not exist, the short and easily understood program to display the time (above) would be impossible. We would have to deal directly with instructions written in machine language. There would be many of them and they would be in a language which is very hard for humans to understand.

Programming abstraction doesn’t stop there. We can define new abstractions of our own. For example the following function prints the n times table up to 9 times n.”

def timesTable(n):

for m in range(1, 10):

print(m, 'times', n, 'is', m*n)

Once this function is defined we can call it whenever we like without having to bother again with the details.


In fact we can create a Python module that encapsulates this function, so the detail is completely hidden! Suppose we name the module demo. Then the timesTable function can be used from a different Python program, like this:

from demo import timesTable


But now we can define a new function that depends on timesTable. The function printUsersTimesTable below prompts the user for a number and then uses timesTable to print the corresponding times table (see Figure 15).

Figure 15 The result of the function printUsersTimesTable() when user enters the number 6

def printUsersTimesTable():

aNumber = input('Please enter your number: ')


So we can build a hierarchy with any number of layers of abstraction, with the detail of each layer hidden from those above and each layer – apart from the bottom one – interacting with the layer below via its interface.

Described image
Figure 16 A hierarchy of layers of abstraction (as encapsulation)

The overall picture that emerges is one of a hierarchy of layers. As illustrated in Figure 14, in such a hierarchy Layer 0 hides its details from Layer 1 above it, Layer 1 hides its details from Layer 2, etc. Conversely, Layer 1 depends on Layer 0, Layer 2 depends on Layer 1, etc.

Figure 14 also shows that such a hierarchy eventually bottoms out at Layer 0. That is the layer at which the abstraction is implemented in physical reality, i.e. a substrate/hardware. This physical layer constrains what is possible at the layers that are built on top of it. Nowadays, most computers are built using electronic components. This is, however, by no means the only possible physical substrate. When Charles Babbage (1791–1871) designed the first programmable computer, he came up with a steam-powered machine built of brass and iron. Babbage never finished his machine, but recently part of it has been constructed using Meccano (see Figure 17).

Described image
Figure 17 Part of Babbage’s Analytical Engine constructed from Meccano

Without going into detail, the idea of layers of abstraction in computing is not restricted to programming languages. It extends, for example, to operating systems and computer networks. In both, a distinction is made between physical and logical layers. Actual devices (e.g. hard drives and computers) and networking hardware (e.g. copper wires) are part of the physical layer. The logical layer contains abstractions (such as IP addresses and protocols).