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



25 responses so far ↓
mohan // November 13, 2006 at 12:00 pm
CONGRALUTATION
Karuna // November 16, 2006 at 6:02 am
Good Job.
concepts are very clearly explained
ashish // December 6, 2006 at 8:30 am
Excellent material
mythili // December 14, 2006 at 4:15 pm
simply good
Ramachandran // January 6, 2007 at 9:51 am
Learnable for the Beginners
Isaac // January 12, 2007 at 8:54 am
Very nice materials about OOPS
vaithi // January 12, 2007 at 8:26 pm
good.
Nice and clear cut explanation
Mohammed Suleman // January 17, 2007 at 5:26 am
Excellent
Kumaresh Babu v c // March 27, 2007 at 6:36 am
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..
Arumugam // July 4, 2007 at 6:18 am
The difference b/w encapsulation and abstraction are explained very clearly..
Useful for Freshers to face interview.
Nice work!!
Arumugam // July 4, 2007 at 6:19 am
The difference b/w encapsulation and abstraction are explained very clearly..
Useful for Freshers to face interview.
Nice work!!!
sam // July 26, 2007 at 2:48 am
examples are nice.
Thanks
Satyam // August 22, 2007 at 3:17 am
Really examples are very good…
shekhar // November 28, 2007 at 10:24 am
hey excellent !!!!!!!!
I was lookin for very easy def for encapsulation n abstraction… thanks @
Good Credit // January 5, 2008 at 6:47 pm
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
priya // March 28, 2008 at 10:17 am
very useful….gud explanation……….
vignesh // July 30, 2008 at 7:55 am
good and veryuseful
raghavendra // July 30, 2008 at 7:56 am
Useful for Freshers to face interview.
great …..
zEsHaN // August 21, 2008 at 9:09 am
So , admirable
Very nice explanation and gud examples given.
thnx..
ravi // January 7, 2009 at 11:56 am
thx for the article it really helped me
arlon // January 14, 2009 at 12:42 pm
“Two thumbs up!”
l // February 3, 2009 at 4:15 am
bnbnbnb
Syed hasan imam // March 25, 2009 at 2:29 am
excellent and meaningful.
irfan // August 11, 2009 at 1:12 pm
nice
will plz give me C AND C++ interview ques, and ans for that ques and also complete theory of C and C++
Mohammad Danish // August 18, 2009 at 4:19 am
can anyone here give an example code it will help me a lot…….!