HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Object Oriented Programming / OOPs – Encapsulation vs Abstraction

OOPs – Encapsulation vs Abstraction

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 !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Anubhav Agarwal

    July 22, 2018

    nice but can you give some code on it .It will help me a lot

  2. Viji

    May 16, 2016

    Can you please explain why we use interface in design patterns instead of concrete classes?

  3. Palash Kanti Kundu

    November 17, 2015

    Nice article. Can you please add some code with your article ?

    That would be a great help for programmers to get into the concept.

  4. Sandip

    April 24, 2015

    Now i got the somewhat clear idea about encapsulation and abstraction.

  5. Ranganath

    November 28, 2014

    Good explanation on abstraction and encapsulation. Thank you

  6. Abhishek Gaur

    November 17, 2014

    Nice explanation. Kindly add practical examples and code also.

    I also found good article here:

    https://stackoverflow.com/questions/11966763/java-encapsulation

  7. swekha

    September 3, 2014

    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

      September 3, 2014

      You type a number and press/touch “DIAL” button. This dial button is actually an example of abstraction of making call.

  8. amit

    July 26, 2014

    is encapsulation and data hiding the same ?

    • Lokesh Gupta

      July 26, 2014

      In layman term, yes data hiding is encapsulation. But, encapsulation is not only data hiding but much more than that.

    • Lokesh Gupta

      July 26, 2014

      I think it’s good guide: https://github.com/javaee/jaxb-v2

  9. HIMANSU NAYAK

    May 4, 2014

    Hi Lokesh,
    Well explained…
    Even Single Responsibility Principal can be imposed better on class using Encapsulation

  10. sreenath ravva

    March 24, 2014

    Can we say abstraction is a thought process and encapsulation is an implementation of abstraction ???

    • Lokesh Gupta

      March 24, 2014

      Not really. Both are parts of implementation. In fact, abstraction is mostly represented via interfaces and encapsulation via implemeting classses.

      • sreenath ravva

        March 24, 2014

        ok, its clear now, Thank you!!

  11. Shivangi Nigam

    January 24, 2014

    You made it easy to remember 🙂

  12. Satish

    January 2, 2014

    Hi, can u please explain it with an example, not wit real time objects, which we use in our programs,

    Thanks in advance.

  13. ajay

    October 29, 2013

    Nice explained. …

  14. kamini

    October 4, 2013

    Can you please tell me why we need all 4 oops concepts in programing i know little but still confused.. please

    • Lokesh Gupta

      October 4, 2013

      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.

  15. subbareddy

    July 20, 2013

    good explanation…..

  16. subbareddy

    July 20, 2013

    good explanation…..

  17. Marko

    July 15, 2013

    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.

Comments are closed on this article!

Search Tutorials

Java OOP

  • OOP – Introduction
  • OOP – Access Modifiers
  • OOP – Constructors
  • OOP – Instance Initializers
  • OOP – Abstraction
  • OOP – Encapsulation
  • OOP – Inheritance
  • OOP – Polymorphism
  • OOP – Overloading vs Overriding
  • OOP – Interface vs Abstract Class
  • OOP – extends vs implements
  • OOP – instanceof operator
  • OOP – Multiple Inheritance
  • Association, Aggregation and Composition

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)