OOP Concept – Encapsulation

Overview

Encapsulation allows an object to separate its interface from its implementation. The data and the implementation code for the object are hidden behind its interface.

Encapsulation hides internal implementation details from users.

Example:
A Customer may issue a check and now know how it is processed. The internal processing is hidden from the customer. Similary, the inessential attributes of a class are hidden from users by using encapsulation. The hidden attributes of a class are called protected attributes.

It ensures that an object supplies only the requested information to another object and hides inessential information.

Example: When a user selects a command from a menu in an application, the code used to perform the actions of that command is hidden from the user.

Encapsulation packages the data and method of an object and provides protection from external tampering by users. It implies that there is visibility to the functionalities offered by an object, and no visibility to its data. The best application of encapsulation is making the data fields private and using public accessor methods.

However, you cannot hide an entire object. To use an object, a part of it needs to be accessed by users. To provide this access, abstraction is used. Abstraction provides access to a specific part of data while encapsulation hides the data. Therefore, abstraction and encapsulation compliment each other.

In OOP, abstraction defines the conceptual boundaries of an object. These boundaries distinguish an object from another objects.

Example: When a user designs an application by using existing templates, the complexity of the template is hidden from the user, but the essential features for creating the application are provided to the user. Abstraction enables an object to display these essential features to develop an application.

Encapsulation and Inheritance are two key concepts in OOP that serve different purposes. While encapsulation hides inessential information, inheritance allows the object of a class to adopt the attributes of another class.

When you implement inheritance in an application, the classes in the application are arranged in a strict hierarchy. The classes at the lower levels inherit the attributes of the classes at the higher levels.

In addition to inheriting the characteristics of the parent class, a class may have its own attributes. This feature implies that the code and characteristics of a class are reusable and extensible. However, inheritance compromises encapsulation because a subclass can directly access the parent’s protected attributes without using operations.

Related Post: Introduction to OOP | Inheritance | Polymorphism

About these ads

44 responses to “OOP Concept – Encapsulation

  1. Good Job.
    concepts are very clearly explained

  2. Excellent material

  3. simply good

  4. Learnable for the Beginners

  5. Very nice materials about OOPS

  6. good.
    Nice and clear cut explanation

  7. Mohammed Suleman

    Excellent

  8. Kumaresh Babu v c

    very nice.easy to understand.if i would have seen this a day before i would have answered clearly in a technical round.Anyway nice..

  9. The difference b/w encapsulation and abstraction are explained very clearly..

    Useful for Freshers to face interview.

    Nice work!!

  10. The difference b/w encapsulation and abstraction are explained very clearly..

    Useful for Freshers to face interview.

    Nice work!!!

  11. examples are nice.
    Thanks

  12. Really examples are very good…

  13. hey excellent !!!!!!!!
    I was lookin for very easy def for encapsulation n abstraction… thanks @

  14. For me as a consumer all these rewards are very confusing. Everytime I look at a credit card deal I feel that there is some kind of a catch but I am not sure what it is. I feel more confident applying for a credit card online from websites that give you a list of different cards so I can compare every point. Lately I’ve been using

    discover lifetime 0 apr

    R56ma87de

  15. very useful….gud explanation……….

  16. good and veryuseful

  17. raghavendra

    Useful for Freshers to face interview.

    great …..

  18. So , admirable
    Very nice explanation and gud examples given.
    thnx..

  19. thx for the article it really helped me

  20. “Two thumbs up!”

  21. Syed hasan imam

    excellent and meaningful.

  22. nice

    will plz give me C AND C++ interview ques, and ans for that ques and also complete theory of C and C++

  23. Mohammad Danish

    can anyone here give an example code it will help me a lot…….!

  24. That’s just superb!

  25. please explain with code examples also…

  26. good description, i will apply it in the fill i will work on

  27. very very good… and example are very nice….

  28. This is good material for OOP

  29. Pharmacy Technician employment opportunities are abundant. Pharmacy technicians work in a number of labor settings including retail pharmacies such as CVS, Walgreens and Osco, hospitals and elderly care amenities. The hrs are sometimes diverse from the “typical” 9-5 online business hours, generally requiring evening buy propecia and weekend do the job. Demand really powerful for the up coming five-10 decades for the reason that the newborn boomer era is coming into the stage of their lives in which they are additional possible to use prescriptions.

  30. plzzzz add program also…..

  31. Very clear. Thanks for it!

  32. i m satisfied…. thnx….thnx a lot…!!!!!…:):))))))

  33. What?s Taking place i am new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I am hoping to contribute & assist other customers like its aided me. Good job.

  34. Great points altogether, you simply won a emblem new reader. What may you recommend in regards to your put up that you simply made some days in the past? Any certain?

  35. thank you very much very use full information …

  36. clear explanation.

  37. well explained, thank you

  38. one example of encapsulation

  39. Thimira Bogahawaththa

    It’s nice lesson I understood what is encapsulation.
    Thank You

  40. fuck you……this is great,thanx alot.

  41. very nice…clear explanation….thank u

  42. Like all medicines, Caverta (Sildenafil citrate) can cause some side effects. These effects are usually mild to moderate and usually don’t last longer than a few hours. Some of these side effects are more likely to occur with higher doses.
    The most common side effects of this medication are headache, flushing of the face and upset stomach.
    Less common side effects that may occur are temporary changes in color vision (such as trouble telling the difference between blue and green objects or having a blue color tinge to them), eyes being more sensitive to light or blurred vision.
    http://www.shoppharmarx.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s