Continuing the Object Oriented Programming series, after writing about Abstraction, it’s time to educate about Encapsulation.
Let’s first look at what is generally believed as Encapsulation? Ask your teacher, “What is Encapsulation?” You teacher would promptly say “Hiding data“. Try googling, you find just 10% of the sites will be giving you right information about Encapsulation where most of them tends to miss out the important glitch. When one of my friends was speaking about Encapsulation, he was right in quoting the proper definition i.e. “Hiding Information”, but his understanding was not correct. If you are one of them who doesn’t understand about Encapsulation, then this article is for you.
So, what exactly the encapsulation means bro? Ok. Let we have some coffee, and we continue discuss about Encapsulation.

Cool, the coffee vending machine has given tasty brewed coffees. Nice coffee! Back to our discussion, here is a simple question I’d like to ask you. Why don’t you give a thought on how did this coffee vending machine worked?
You dropped in the coin, and the coffee came out of the machine in a beautiful coffee cup. So, why didn’t you see how the where the coffee beans, how it is prepared? You really don’t know (unless you are the one who designed the machine
) how things are made inside the machine, but finally you get a coffee!! Yeah don’t you see a sense of Encapsulation? So, here what is actually hidden? The Coffee beans? No, the way how the coffee prepared is hidden.
So, Encapsulation is hiding of process; hence you hide the data. Not the vice versa.
From the definition, Encapsulation is “Information Hiding”, the information is how something is happening inside an object? It’s just a mere data hiding concept (though it includes data hiding).
So, when you model this coffee vending machine, you don’t want to show out the way how a coffee is made. Isn’t it? So, you don’t want to show where the coffee beans, milk and the sugar are. Right?

So, in the CofeeVendingMachine class, prepareCoffee() is hidden and getCoffee() is made public, so that you will be able to send a request to the machine.
Since you have hidden the process, you don’t have any need to show how the coffee & milk are mixed and how it’s been brewed. If you need a stylish design, even you can show that.

The Coffee Vending Machine encapsulates the internal process and the ingredients (data) used in the process.
Object Oriented Languages provides encapsulation via modifiers such as “private”, “protected”. Sometimes, volatile and transient also acts as encapsulating modifiers, but they are language specifics.
Encapsulation doesn’t stop just with OOP languages. The concept extends itself to the latest technology such as Webservices, Service Oriented Architecture. If you closely watch, any real world object has encapsulation in it, which the Object Oriented Programming tries to model.
13 Comments
HI mahesh,
information is too gud…. Please send anything like this to my office ID.. getting bored in office
Hi Vijay, that’s great that you liked it ( guess not because you are bored
). And also I’m editing your comments to remove the email id as, spam bots might track your Id and start spamming you
.
Very useful information.
Raj, thanks for your comments
Your suggestions are always welcomed!
wow..This is really great
Thanks for making this!
Great!! simple and easy to understand.
If all information was as clear as this one,.. then im sure more people are going to rise in their fields,…..
very clear very informative,…
i hope you have other posts like this one,…
Wow.
Wonderful explanations. I am a programmer and a PhD student and I love OOP programming. Your tutorial and way of explaining are great, will learn them and use them in my teaching. Great Job! Would love to receive more of this
Good for beginners.
Perhaps you’d want to expand on this subject for a matured audience (since it is ~ 2 years from your original post).
Thats the knowledge which everyone seek.
Really a nice one for any enterprenuere.
A professor at my university taught us the concept using a similar anecdote, just with a soda machine instead of a coffee machine. He told us that for all we know, there’s a monkey inside the machine counting our change and dispensing our drinks.
The only thing I don’t agree with is how volatile and transient can be used to encapsulate processes. Volatile is used for concurrency and transient is used for data storage. I know it’s been a while since you posted this, but if you don’t mind me asking, how do these keywords help with encapsulation?
Hi It’s Really very very very nice information I am finding exactly such article, nice example. Thanking you..
One Trackback
[...] If you don’t know, or perhaps don’t fully understand, what encapsulation is, read this article for a solid explanation. Encapsulation allows you to change implementation details with minimal [...]