Encapsulation and information hiding are key concepts used in object-oriented programming. Information hiding means separating the description of how to use a class from the implementation details. The idea of information hiding is that a programmer will be able to use a class we write without needing to know how the methods are implemented. All the programmer needs to know is what they provide to the class, and what they get in return. This keeps the amount of information needed to use a class minimal, meaning in turn that applying our classes is easier to do.
Encapsulation means grouping software into units in such a way that it is easy to use with well-defined simple interfaces. Encapsulation and information hiding are related to each other, since both have a goal of making classes easier to use. The way that we make classes easy to use is by hiding the details of the implementation. This, in turn, makes the interfaces that face the user easier to understand.
Hiding details from the user of a class can be done with scope. When we have publically scoped variables and functions, anything outside a class can access them. If we want to hide variables and functions, we simply make them private scope instead. When we are implementing encapsulation, we will typically make all property variables private, and define public methods to be able to set and get their values, known as accessor and mutator methods. As an example, let’s take a look at a typical Stack implementation.
We have a property that tracks the top of the stack, and this is a private variable. In order for the user to interact with it, they must use one of our public methods, push, peek, or pop. There are two main advantages to this class setup:
1. Although we store data in Nodes, when the user pushes onto the stack, they pass a variable of the type they wish to store on it. Similarly, when they pop, they receive back a variable of the same type. They don’t need to know anything about us storing the data in Nodes, since those details are hidden from their view. All they need to know is how to use push, peek, and pop.
2. We have complete control over how the user manipulates the contents of the stack. If they had public access to the top, they could change it in a way that breaks the properties of the stack. Since the top is hidden from their access, they have no way of breaking the stack, as long as our code is correct.
Since we control how the user interacts with private variables, we can more comfortably make assumptions about the format of private variables. When the user chooses to set the variables, we can make sure the values conform to our expectations before we allow the value to be updated. This idea is essential for APIs, and related concepts. Since we are opening a way for a user to interact with our application and data, we want to make sure they are not passing malicious or malformed data into our classes. Using encapsulation is one way to protect ourselves from bad user data.