Image Credits to Pixabay at Pexels

The 4 Principles of OOP — Part 2: Encapsulation

Aimee
3 min readJun 28, 2021

--

In Part 1 of this series, we explored Inheritance, or the ability for objects to be able to retain certain properties and attributes from a parent. This differs depending on whether we’re using class inheritance with object-oriented languages, such as Python, Java, C++, Ruby, or prototypal, of which JavaScript is most famously an example.

In Part 2, I’ll be reviewing the second principle, encapsulation. As a reminder, there are four principles to Object Oriented Programming.

Thorben Janssen defines encapsulation as “the idea of bundling data and methods that work on that data within one unit”. It does so by allowing some data and methods to be accessed through what it called information hiding. While it varies across the object-oriented programming languages, there are a number of methods that can be used to view and change data. Most commonly known are getter and setter methods. Getter methods “get” the information: they are able to read the data, but they cannot change the data. Setter methods “set” the information: they do not read the data, but have access to write/re-write the data.

In Ruby, there are also private methods, which are defined by prefacing the method with the word private before it. This ensures that the method is unique to the class, and can't be accessed by other classes.

Encapsulation in JavaScript, which is a prototypal language, is different. Let’s use the below as an example:

let dog = {
name: "Max",
age: 2,
favorite_toy: "Frisbee"
}
console.log(dog["name"]) /* this will return "Max" */

We’ve instantiated a new dog object where the dog’s name is Max. Max’s favorite toy is a Frisbee. As we know, though, you can reassign values in JavaScript, so this will also work:

let dog = {
name: "Max",
age: 2,
favorite_toy: "Frisbee"
}
console.log(dog["name"]) /* this will return "Max" */
dog["name"] = "Charlie"
console.log(dog["name"]) /* this will return "Charlie" */

Which is it then? Is the dog’s name Max, or is it Charlie?

JavaScript doesn’t use setter and getter methods like Object-Oriented languages do, because it allows for variables and objects to be reassigned its values. That’s why some solutions to address this might be:

  1. Using anonymous functions and closures. This prevents our data from being accessed by functions that are not within the scope, and the data as well as its values will be retained in memory for usage.
  2. Our other option would be using constructors and the keyword this. By doing so, we're constructing a method or some sort of data, and the this points directly to what's within the scope of this function.

By allowing us to determine whether we want our methods to get, set, allow for both, or allow for neither to be by another party, encapsulation (in Object-Oriented languages) gives us better control of our information. To address this, JavaScript offers its own unique approaches: we can use constructors and this, or use anonymous functions and closures to determine what, if anything, can and should be changed or read.

Please check out the below resources if you want to learn more about encapsulation. Next in this series will be abstraction.

--

--