OOP Concepts, Encapsulation.

Hasan Ozyer
4 min readJun 20, 2023

--

Encapsulation in Java. It is one of the most essential concept in OOP. Encapsulation allows code to be organized into modular and self-contained parts, which provides benefits such as data security, code reuse, and simpler maintenance. However, this definition does not matter because i strongly stand by the saying of Albert Einstein; “You do not really understand something unless you can explain it to your grandmother.” So, listen grandma, i am about to explain you what does encapsulation means. So sit tight, and read carefully.

Albert

For My Grandma.

So Grandma. Let’s not look at young and handsome Einstein plz, he died years ago. Getting back to our topic, ENCAPSULATION. I always like to explain with examples first. Let’s think about the real world.

Listen grandma, You know atm machines right?

💡 The ATM machine is covered with layers of security, someone who does not have your credit card can not access to your assets.

💡 It is protected from outsiders to came and take your money without your permission.

💡 Your money is packaged and encapsulated safely.

💡 You removed the access of others to your money. It is hidden.

So now, think grandma! think outside the box! why do they cover your valuable money with layers of security? Why are they doing that?!?

There is various answers to this question dear grandma, but i see that you already napping in front of me so, i am just gonna move on to explain to my audience. Which there are none so i am just gonna pretened i have a bunch of Software Developers or Software QA Engineers following me and reading my stories.

Eugene.

For You, SDET wannabe(s) Hasan(s).

Alright coding world, let’s teach some encapsulation to me. While writing code, if you are like me, as a beginner you write everything in public. Which means that everything can be accessed from someone else in the same project.

Yea, so what is the problem with that? the junior me says.

Have you ever heard of spaghetti code? i respond, if you always write you codes with no encapsulation, your code will probably be hard to read, hard to trace and therefore hard to track. Just a big load of SPAGHETTI.

Now, i will prove to myself that grandma saving her money in atm machine relates to encapsulation in Java. Because why not.

Now Hasan, think that Grandma putting her money in an ATM machine. The ATM machine can be viewed as an object that encapsulates both data and methods for dealing with your Grandma’s money.

▶️ Encapsulation of The Data: There are private variables or data fields inside the ATM machine that hold your Grandma’s information, such as her account number, PIN, and current balance. Now Hasan, These variables are encapsulated, which means they are protected from outside access (e.g. you) and can only be accessed or modified via predefined methods.

▶️ Encapsulation of The Method: Hasan, the ATM machine provides a wide range of functions (methods) to engage with your grandmother’s money. For example, a “withdrawMoney” function (method) may receive an amount as input and subtract it from your Grandma’s account balance. likewise, a “deposit” function that adds money to her account may exist. These methods are wrapped within the ATM object, which means that they can only be accessible through a specific interactions that ATM machine provides( maybe API’s).

💡Conclusion 1: Grandma’s money has been protected by encapsulating her money (data) within the ATM object. The private data fields cannot be accessed or changed directly from outside of the object, guaranteeing the security of her account information. The ATM object’s functions enable Grandma to interact with her money in a controlled and secure manner.

💡Conclusion 2: Mr. Hasan, there is also another aspect of this, which is that Encapsulation also allows for code reusability. The ATM object can be reused in multiple contexts or not just by your granny, but also from different users as long as the established interface is followed. It also simplifies maintenance since any modifications or updates that are required may be done within the encapsulated object without impacting the external code that your granny or other users that uses the ATM machine.

Overall, in this example, Mr. Hasan, you tried to explain yourself that encapsulation shows how your Grandma’s money are bundled together within the ATM object, which in this case, points towards to encapsulation, giving security, reusability, and maintainability; To the money, and to your code.

--

--

Hasan Ozyer

Junior Test Automation Engineer. Documenting my IT journey for my future self. Sharing my opinions, learnings, and adventures in the tech world.