Member-only story

Undestanding Encapsulation and Information Hiding

Scott Cosentino
3 min readDec 4, 2019

--

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.

--

--

Scott Cosentino
Scott Cosentino

No responses yet