Object Oriented Programming – Encapsulation is not just hiding data!

June 9th, 2008

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

  1. Vijay
    Posted June 11, 2008 at 10:40 am | Permalink

    HI mahesh,

    information is too gud…. Please send anything like this to my office ID.. getting bored in office :)

  2. Posted June 11, 2008 at 11:54 am | Permalink

    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 :-) .

  3. Raj
    Posted August 26, 2008 at 10:01 pm | Permalink

    Very useful information.

  4. Posted August 27, 2008 at 6:45 am | Permalink

    Raj, thanks for your comments :-)
    Your suggestions are always welcomed!

  5. chupao
    Posted July 29, 2009 at 6:40 am | Permalink

    wow..This is really great :D Thanks for making this!

  6. Vatsav
    Posted October 16, 2009 at 7:33 am | Permalink

    Great!! simple and easy to understand.

  7. Testgamma1
    Posted November 10, 2009 at 6:19 pm | Permalink

    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,…

  8. Venkat
    Posted September 16, 2010 at 12:36 am | Permalink

    Wow.

  9. Carla Muntean
    Posted November 8, 2010 at 1:40 pm | Permalink

    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

  10. Bharath
    Posted January 4, 2011 at 9:59 pm | Permalink

    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).

  11. Prashant
    Posted January 4, 2011 at 10:43 pm | Permalink

    Thats the knowledge which everyone seek.
    Really a nice one for any enterprenuere.

  12. Posted August 15, 2011 at 1:39 pm | Permalink

    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?

  13. Ankita
    Posted January 18, 2012 at 7:29 am | Permalink

    Hi It’s Really very very very nice information I am finding exactly such article, nice example. Thanking you..

One Trackback

  1. [...] 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 [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*