OOP Concepts — Encapsulation

Elijah Wines
4 min readDec 6, 2022

--

In today’s article we will be discussing Encapsulation, another one of the 4 pillars of Object Oriented Programming. I will be discussing what encapsulation is, its benefits, and a real life coding example utilizing Encapsulation. Before we get into it, let’s talk a bit about Object Oriented Programming.

Object Oriented Programming

Object Oriented Programming is a model that organizes software design around objects. It allows us to focus on the objects that we want to manipulate, rather than the functions we use to manipulate them. This is very useful when creating large applications that are complex and actively maintained by developers. OOP is broken down into 4 key components: Classes, Objects, Methods, and Attributes. Together, these components can successfully work together to create code that is well-structured, easy to read, and reusable.

There are 4 pillars that hold up Object Oriented Programming that you should always keep in mind when following the OOP model. Today, we will be discussing Encapsulation, which is one of those 4 pillars.

Encapsulation

What is Encapsulation?

Encapsulation is the process of combining data and functions into a single unit, which we call and object. The purpose of this is to protect data so that only the objects methods can access the properties within that class. This way, we can pick an choose what we want to display to the end-user, rather than laying all of our work flat out on the table for everyone to see.

Why Use Encapsulation?

Encapsulation is typically used to hide the implementation details of a class from the user. In doing so, we also allow the programmer to change internal methods and functionalities of the class without affecting any outside code that uses the class. This allows the code to be more maintainable because changes are isolated. A common problem when creating large applications is that you don’t want one small change to cause a whole array of issues with the rest of your code. All of this can be avoided when following Encapsulation.

What Are The Benefits of Encapsulation?

  1. Details of your implementation are hidden from the outside world. This makes the code more secure and easier to understand
  2. Allows the programmer to change a classes internal code without affecting other code within your project that uses it. This allows your code to be more maintainable.
  3. Allows the programmer to control how the data/properties of a class can be accessed and modified, which is another way Encapsulation can improve security.
  4. Promotes modularity and reusability because objects can be easily combined and reused in different areas of your application.

Overall, Encapsulation is a great tool for organizing and protecting data within your application.

Example

A good example of Encapsulation would be a class that represents a bank account. In this class, we will have properties that represent the account owner and the account balance. This is sensitive data that we don’t want just anyone to see, so Encapsulation can help protect this information. We will also have some methods within this class that represent withdrawing or depositing money. Later on, we may have some outside code that utilizes these methods. By keeping these methods “encapsulated”, we can easily go back and edit these methods without having to also revisit all the other places in our code that may utilize them.

Let’s take a look at this example using JavaScript.

First, let’s create our Account class:

class Account {

// the '#' symbol at the beginning of a variable indicates a private property
#balance = 0;
#ownerName = "";

constructor(ownerName) {
this.#ownerName = ownerName;
};

// our 'get' methods are the only way we can access our private properties from outside of the class
getOwnerName() {
return this.#ownerName;
};

getBalance() {
return this.#balance;
};

// methods to deposit or withdraw money from our bank account
deposit(amount) {
this.#balance += amount;
return `Balance after deposit: ${this.#balance}`;
};

withdraw(amount) {
this.#balance -= amount;
return `Balance after withdraw: ${this.#balance}`;
};

}

Next, let’s see our class in action!

To start off, let’s see how we can access our data from outside the class:

var account = new Account("Elijah");

console.log(account.#ownerName) // throws an error
console.log(account.getOwnerName()) // returns "Elijah"

console.log(account.#balance) // throws and error
console.log(account.getBalance()) // returns #balance property

As you can see, we can create properties within our class that can only be accessed from within our class. If we want to make that property visible from outside of the class, we have to create our get methods.

Now, let’s take a look at our deposit and withdraw methods in action:

console.log(account.deposit(500)); // returns "Balance after deposit: 500"
console.log(account.withdraw(200)); // returns "Balance after withdraw: 300"

Here, we can easily create methods to perform tasks using our data within the class. These implementations are hidden from the end user, and only returns to the user what they need to see.

Conclusion

In today’s article, we discussed Encapsulation, One of the 4 pillars of Object Oriented Programming that emphasizes keeping our data and functions all together within a single object. We talked about what it is, why to use it, and an real world example using JavaScript. Encapsulation is a very important aspect of Object Oriented Programming, and is an essential topic that helps you keep your code clean and maintainable. I hope you learned something new today and keep encapsulation in mind during future projects. Keep learning, and happy coding!

--

--

Elijah Wines

Software Engineer - JavaScript, Ruby, React, Ruby on Rails