Most of you will agree that encapsulation and abstraction together brings a lot of confusion. Most blogs add only confusion further. Lets solve this puzzle.
I started working on this post after my previous post “Understanding abstraction in java“. My goal was to understand encapsulation in java and how it relates to abstraction. As soon as i began, i started going down in more confusion like never before. After browsing many hours and reading some really well written as well as confusing blog entries, i was able to make out some clear understanding. Follow my footprints..
Table of Contents Encapsulation in simple words Encapsulation in Detail Encapsulation vs Abstraction
Encapsulation in simple words
Wrapping data and methods within classes in combination with implementation hiding (through access control) is often called encapsulation. The result is a data type with characteristics and behaviors. Encapsulation essentially has both i.e. information hiding and implementation hiding.
Encapsulation in Detail
I read it somewhere : “Whatever changes, encapsulate it“. It has been quoted as a famous design principle. For that matter in any class, changes can happen in data in runtime and changes in implementation can happen in future releases. So, encapsulation applies to both i.e. data as well as implementation.
Access control or implementation hiding puts boundaries within a data type or class for two important reasons. The first is to establish what the client programmers can and can’t use. This feeds directly into the second reason, which is to separate the interface from the implementation.
If you are sure that client programmers can’t do anything but send messages to the public interface, then you are free to change anything that’s not public (e.g., package access, protected, or private) without breaking client code. Encapsulation helps you in achieving this surety.
Encapsulation vs Abstraction
If you have gone through my last post, you will see that abstraction is essentially an idea, which helps in setting the guidelines. Encapsulation is the mechanism by which we achieve the desired abstraction.
In short, from OOAD perspective:
- Abstraction is more about ‘What‘ a class can do. [Idea]
- Encapsulation is more about ‘How‘ to achieve that functionality. [Implementation]
I have seen many contradictions to this theory over many blogs. So, if you also don’t agree with this, please bear with me. Also, i will request you to put a comment you idea related to topic. I will happily try to relate or negate.
Going forward, i will take example of our well known class HashMap. This class is responsible for storing key-value pair, searching based on key and do more things. From outside, client code only knows the method names and their behavior. It calls these methods and live happily. This is actually what abstraction guidelines are. Abstraction says that client code should call a method to add key-value pair, a method to retrieve value based on key and so on. How it should be done? is not business of abstraction.
And here comes encapsulation, when you start writing actual code. You write HashMap.Entry class and create variable table of type Entry[]
. Then you declare all such things private and give public access to only put()
and get()
methods etc. This is actually encapsulation. A realization of your desired abstraction.
I hope that you have more clarity about java encapsulation and it’s difference with abstraction.
Happy learning !!
Anubhav Agarwal
nice but can you give some code on it .It will help me a lot
Viji
Can you please explain why we use interface in design patterns instead of concrete classes?
Palash Kanti Kundu
Nice article. Can you please add some code with your article ?
That would be a great help for programmers to get into the concept.
Sandip
Now i got the somewhat clear idea about encapsulation and abstraction.
Ranganath
Good explanation on abstraction and encapsulation. Thank you
Abhishek Gaur
Nice explanation. Kindly add practical examples and code also.
I also found good article here:
https://stackoverflow.com/questions/11966763/java-encapsulation
swekha
hiii,I have one question.Let consider mobile ph. example for understanding abstraction and encapsulation.we have keypad.we don’t know the internal implementation of various circuits with keypad.but we have the option to use this for dialling.this is known as encapsulation.then my question is what is abstraction here?????please give me example on mobile ph.
Lokesh Gupta
You type a number and press/touch “DIAL” button. This dial button is actually an example of abstraction of making call.
amit
is encapsulation and data hiding the same ?
Lokesh Gupta
In layman term, yes data hiding is encapsulation. But, encapsulation is not only data hiding but much more than that.
Lokesh Gupta
I think it’s good guide: https://github.com/javaee/jaxb-v2
HIMANSU NAYAK
Hi Lokesh,
Well explained…
Even Single Responsibility Principal can be imposed better on class using Encapsulation
sreenath ravva
Can we say abstraction is a thought process and encapsulation is an implementation of abstraction ???
Lokesh Gupta
Not really. Both are parts of implementation. In fact, abstraction is mostly represented via interfaces and encapsulation via implemeting classses.
sreenath ravva
ok, its clear now, Thank you!!
Shivangi Nigam
You made it easy to remember 🙂
Satish
Hi, can u please explain it with an example, not wit real time objects, which we use in our programs,
Thanks in advance.
ajay
Nice explained. …
kamini
Can you please tell me why we need all 4 oops concepts in programing i know little but still confused.. please
Lokesh Gupta
You don’t need to have. You write any program, and it will have all fours. These are the building blocks of language. You can’t write your program like I will use only 3, and not last fourth. It does not happen.
subbareddy
good explanation…..
subbareddy
good explanation…..
Marko
It has been a very mis-understood concept ever in java. You have concluded it very effectively and in simple words which are easy to understood by all. Great post.