Wait! Is it Encapsulation/ Abstraction?

Explained using two simple and real-life examples

I believe, to programmers, these are the two famous and favorite friends. Also, one of the many interview questions. Many would know them theoretically and many would practically be implementing them in their programs too, but even after spending enough time, if you still show the following symptoms:

  • Are they both similar?
  • I know them by the bookish definitions, but sometimes I don’t get, how different they are?
  • Are they really two separate concepts? Because I feel they’re one and the same.
  • After looking at the program, I can’t really say, which is Encapsulation and which is Abstraction here?
  • If I have to write a program in any language, how do I implement these concepts?

don’t worry you’re just fine! I would like to share two real-world examples that are very easy to relate and try to solve the confusion.

Example 1: A Simple Light Switch

When you turn the switch ON/ OFF

I know you must have noticed the switches in your house/ office/ school. You know the light switch in your living/ a bedroom right? Let us just put down the points and details we know all about that particular switch

Purpose/ Usage of the switch

The purpose of the switch is simple. It’s just to light up the room when needed.

How to use operate it?

  • The switch has just two options ON and OFF. You just have to press it.
  • When pressed down/ turned ON, it lights up by sending power/ electricity to the Bulb and lights up the room
  • When pressed up/ turned OFF, it cuts off the power source to the bulb and turns the room dark

Types of users?

There can be two kinds of users here

  • An electrician who would come and install the switch for you. He is very well qualified and have skills in the area and knows every single detail about the switches and stuff and is never afraid of electric shocks. He is the man!
  • A user like me/ you, a regular guy who would just want to use the switch and don’t really care how the switch is installed. Also, I/ you might sometime think, it really doesn’t matter how the electrician would have done the job. We just like the bulb to turn ON/ OFF when pressed.

How do we access the switch?

  • A switchboard containing various buttons (buttons including a switch to the bulb, a regulator to control fan speed, sockets, etc) will be mounted on a wall that is reachable to the human.
  • The wiring required to send the electricity between the power source and the bulb will be embedded in the wall itself without exposing much, for safety purposes. So the user doesn’t accidentally touch the wires carrying electricity.

How do you know it is a switch to light and not to any other?

  • A switch comes in a variety of colors, shapes, and sizes.
  • A switch is usually located in reachable places based on the necessity. For example, a bathroom light switch will be located near the Bathroom and not in the living room. Just like that, a Kitchen light switch will not be located in a bedroom.
  • If there are multiple switches then to one to the light, might have special indication/ markings so the human can easily identify it.

I think we jotted down almost everything we know about a switch, didn’t we? Let’s try to identify what is Encapsulation and Abstraction here.

Abstraction v/s Encapsulation

For a regular person, what is it that you really need to know

  • Where is the light?
  • Where is the switch installed/ located?
  • If there are multiple switches/ buttons on a single switchboard, then pressing which way switch would turn ON/ OFF which light? Like how do I identify which button is for which light?

I guess these are only essential details, which any regular person would need to know. I’m sure even you would agree with me, right?

And you know what, this is what an Abstraction concept is! Only the relevant and required details are presented to the user and the Electrician hid all the irrelevant details which are not really to be accessed by a regular person for safety reasons.

For an electrician who is such a skilled person, while installing the switch would keep following things in mind

  • Based on the usage like the Kitchen/ Bathroom, he knows the best place to install the switch.
  • He knows how to make the wiring keeping all safety measures so that none of the wires are exposed out.
  • If there is a requirement to have switches for controlling fan speed, run a water pump, turn on a light in the living room, all in the same place like the same switchboard then, he will have to come with a plan and design on how to handle all these wirings. Because each electronic components will have different power requirements.
  • He might have to think of different designs based on different requirements based on the relevancy.

I am not an Electrician, but I assume these are points that he would keep in mind while working. Bottom line is that he knows whats is best for a user of that switch.

But you now being an electrician, with all the skills, he knows how to put all the relevant things like wirings, tapes, soldering, etc in just one place called switchboard so that the user doesn’t get to see what’s behind it.

All the relevant things put together in just one place without giving away too many details. And when I say relevant, it means that a Kitchen switchboard wouldn’t contain the wiring for a light present in Bedroom. Keeping all the relevant things together and showing just the essential features to the user is the concept called Encapsulation!

Example 2: An ATM Machine

We all know what this machine is and what is it used for, right? It is an Automated Teller Machine, which dispenses money. Oooh, money!!??? Of course, there are many other features this machine offers. But let’s just list down all the things that we know about the machine.

  • Every bank has its own ATM machines
  • A person should hold an account in a bank and requires a card to use the machine.
  • A person with an account in one bank can use the ATM machine of any other bank too
  • An ATM machine has the following features like
  1. Reads card
  2. Reads security PIN
  3. Reads the amount we want to withdraw
  • Give the user the basic options to
  1. Select the denomination

2. Select the account type as if it is Savings, Current, etc.

3. View balance and many more.

  • The card slot where the user can swipe the card
  • The cash slot, where the notes would be dispensed

I guess these are the essential details that any person who uses the machine would be interested in right? I hope you agree.

Now let’s just put down all the points which we don’t know. Or we would have always wondered about.

  • How does the machine knows my name, when I swipe my card?
  • How does it know that I have entered the right PIN?
  • How does it know whether there is enough cash for the denomination I’ve asked for?
  • Who refills in the cash in the machine when it’s empty?
  • How does it know when to reverse the transaction when something goes wrong?
  • How do my transactions notify me by sending SMS on time just when I’m done?

And many other questions that being with How/ When?

I hope these are the points which we know and we really don’t know about the machine.

Now let’s just try to figure out how Encapsulation and Abstraction would fit in here.

Abstraction v/s Encapsulation

If you have read my earlier post, then by now you must have already figured out and kind of guessed what encapsulation and abstraction are here. So, let’s understand what’s going on here.

If you’re a regular or a cool guy just like me then you know, all we really care about ATM machine would be just

  • How to operate it,
  • Get the money,
  • And leave the place silently without anybody noticing. Shhh….!!!

This concept of the machine is Abstraction. Because, we really don’t know what’s going inside the machine, like when I enter my PIN which program is running in the background to verify it. Or let’s say which program is sending me the SMS notifications or how the cash is stored.

Also, if you just think for a moment, these details are kind of protected from the user for any misuses. So, the integrity and confidentiality of the user is protected and is safe in the hands of the bank.

Now, the Bank and the Bank personals who would have manufactured and installed the ATM machine in your area would have done it

  • By maintaining the confidentiality of the user information,
  • Hiding the details from the user like of how the machinery works internally like machinery parts or the programs

The very important details which cannot be compromised are hidden from the user. And this concept is called Encapsulation.

The most important difference between these two concepts is INTENT. The intention of giving away only the relevant and necessary details to the user and hiding the implementation details (which are really not required to the user) from the user.

In a way, if you see, the concept of encapsulation is a way of implementing the abstraction concepts.

I hope by now you know what the Abstraction and Encapsulation concepts are. What do you think?

B.E. Graduate | Lead Developer | Theater-Enthusiast | Blogger | Husband

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store