AQA 7517, section 4.4.1.4
Information hiding is the process of hiding all the details of an object that do not contribute to its essential characteristics. Users of the object see only what they need to use it, and the inner workings are kept out of sight.
Everyday example
When you drive a car, you use the steering wheel, pedals and gear stick. You don't need to know how the engine, fuel injection or gearbox work. Those details are hidden behind a simple interface. The car can have a new engine fitted and you can still drive it in exactly the same way.
In programming
A program uses information hiding when:
- a subroutine hides how it works, and the caller only needs to know its name, parameters and what it returns;
- a class keeps its attributes private, and exposes public methods that are the only way to use them;
- an abstract data type, such as a stack, hides whether it is stored as an array or a linked list.
class Thermostat:
def __init__(self):
self.__target = 20 # hidden detail
def set_target(self, degrees): # the public interface
if 5 <= degrees <= 30:
self.__target = degrees
def get_target(self):
return self.__target
t = Thermostat()
t.set_target(22)
print(t.get_target()) # 22
The user of Thermostat can only change the target through set_target, which checks that the value is sensible. They can't set it to 500 by accident, and they don't need to know how it's stored.
Benefits of information hiding
- Protects data from being changed accidentally or incorrectly.
- Reduces complexity, because a programmer only has to learn the interface.
- Allows the inside to change. The hidden implementation can be improved or replaced without breaking the code that uses it.
- Reduces errors, because parts of the program depend less on each other.
- Supports teamwork, since different people can write the inside and the outside separately.
Information hiding and abstraction
Information hiding is closely related to abstraction. Abstraction focuses on the essential features. Information hiding conceals the rest, so users can't depend on the details even if they wanted to.
Worked example
Task: explain how a stack ADT uses information hiding.
A stack provides push, pop, peek and is_empty. These are the essential characteristics. How the items are actually stored (an array with a pointer, or a linked list) is hidden.
If the programmer decided to change the stack from an array to a linked list for efficiency, no code that uses the stack would have to change, because it only ever used push and pop.
Exam tip: don't just define information hiding. Say what is hidden (the details that don't contribute to the essential characteristics) and why it helps, such as protecting data from accidental change or allowing the implementation to be changed without affecting other code.
Check yourself
What is information hiding?
Hiding all the details of an object that do not contribute to its essential characteristics.
Give two benefits of information hiding
Any two of: it protects data from accidental change; it reduces complexity for the user; the inside can be changed without affecting other code; it reduces errors; it supports teamwork.
How do private attributes in a class apply information hiding?
Other code can't access the data directly. It has to use the public methods, which can check and control the changes.