DEV Community

Cover image for Unboxing OOP: Your Guide to Encapsulation and Abstraction
Josh Carvell
Josh Carvell

Posted on

Unboxing OOP: Your Guide to Encapsulation and Abstraction

It's easy for new developers to get caught up in the excitement of building projects and neglect the less thrilling parts of software development.

After all, who can blame them? For someone getting their teeth stuck into their first few projects, Object Oriented Programming (OOP) concepts can seem obscure and difficult to unpack.

But they are much simpler than they sound! You may already be using some of these concepts without knowing, and they are simple tools to help make sure that your projects get the fundamentals right.

In this article, we'll cut through the jargon and break down encapsulation and abstraction with real-world examples


Encapsulation


Let's say you're going to swim at the local pool. How do you ensure that your personal belongings are safe while you're in the pool?

You store your property in a locker. Only someone with that exact locker key can access whatever you have stored inside it.

Similarly, developers need to be able to control who and what has access to our program's data and methods. This is where encapsulation comes in.

In simple terms, we use encapsulation to put our program's data and methods inside a box (a class), and we only allow the necessary parts to be accessed from outside the box. This makes our code more secure and easier to manage.


Example


Let's imagine we are programming a vending machine that sells chocolate. Each item has a price and a stock level that we need to monitor as people use it.

We need to keep track of the price and stock of each chocolate bar, but we don't want anyone to be able to change the price or stock without permission.

So we put the price and stock variables inside a "Chocolate" class and make them private, which means only the Chocolate class can access them.

Chocolate Class

Then we create public methods to get the price and stock and a method to decrease the stock when someone buys a chocolate bar. This way, we control how the price and stock are accessed and modified while our vending machine is in use.


Abstraction


In programming, we use abstraction to simplify complex systems.

Let's say you're playing a video game.

You don't need to know how the game engine works or how the graphics are rendered or how the in game physics are processed.

As the player, all you need to know is how to control your character and how to interact with the game environment.

This is an example of abstraction. Abstraction means simplifying something complex by only showing the most important parts. It's like giving someone a summary of a long story instead of telling them every detail.


Example


Lets revisit our video game example.

Character class

In this game, our Character class abstracts away the complexity of the game engine and provides a simplified interface for interacting with the character.

The player doesn't need to know exactly what happens under the hood when they press move or jump. All they need to know is how to jump or run within our game.

What's the difference between the two?

In simple terms, encapsulation is about controlling access to the important parts of a system, while abstraction is about simplifying the system by hiding unnecessary details.

Top comments (0)