Difference between Encapsulation and Abstraction in Java

Encapsulation and abstraction are two out of four pillars of object-oriented programming. Both principles help in designing a class so that the class can perform necessary functions, as well as, does not expose unwanted details to the other classes to avoid its misuse.

In this post, we will understand abstraction and encapsulation in detail, and discuss a few differences between the both.

1. Encapsulation in Simple Words

In simple terms, wrapping the data (state) and the methods (behavior) inside a class in combination with information and implementation hiding (through access control) is called encapsulation.

The result of encapsulation is a class with characteristics and behaviors. It can interact with other classes in the application to perform any business operation.

In the following example, the ReportWriter class has defaultLocation attribute that can be accessed via public setter and getter methods. It prevents setting the attribute directly by setting it private.

ReportWriter also provides the writeReport() method that will generate the report in the current default location set in the defaultLocation attribute. It is an example of encapsulation.

class ReportWriter {

  private String defaultLocation;

  public String getDefaultLocation() {
    return defaultLocation;
  }

  public void setDefaultLocation(String defaultLocation) {
    if(defaultLocation != null)
      this.defaultLocation = defaultLocation;
  }

  public void writeReport(String reportType) {
    //...
  }
}

2. Whatever changes, Encapsulate it.

I read it somewhere: “Whatever changes, encapsulate it“. It has been quoted as a famous design principle. For that matter, in any class, the 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.

  • Establishing what other classes can and can’t use.
  • To separate the interface from the implementation.

In any class, if we can ensure that other classes cannot do anything but send messages through the public methods, then we can modify the non-public members of the class in the future without worrying and without breaking the client code. Encapsulation helps us in achieving this surety.

3. What is Abstraction?

Abstraction is the ability to create abstract actors in the system that can perform work, report and change their state, and communicate with other objects in the system. The abstract actor means, we know how to work with an object, but we don’t know how it works internally. It hides its implementation details using the access modifiers.

To work with such abstract actors, we only need to know about the public APIs that we will use. We need not to care about how they perform their work.

For example, in the previous example of ReportWriter, we have defined the writeReport() method that clients must use to generate the reports. Clients must not be concerned about how the reports are generated.

class ReportWriter {

  public void writeReport(String reportType) {
    //...
  }
}

4. Difference between Encapsulation and Abstraction

While learning abstraction, we learned that abstraction is essentially an idea, which helps in abstracting the behavior for a class. Encapsulation is the mechanism by which we implement the desired abstraction.

In short, from OOP perspective, we can say that:

  • Abstraction is more about ‘What‘ a class can do. [Idea]
  • Encapsulation is more about ‘How‘ to achieve that functionality. [Implementation]

Take the example of the well-known class HashMap. The HashMap class is responsible for storing key-value pairs, searching based on keys and doing more things.

From outside, the client code only knows its get() and put() methods. They call these methods and live happily. This is essentially the abstraction. Abstraction says that client code should call a method to add key-value pair, a method to retrieve a value based on a key and so on. How it should be done, is not concerning to the client code.

The encapsulation is when we implement the HashMap class and write these methods. We write HashMap.Entry class and create a variable table of type Entry[]. Then we declare all such things private and give public access to only put() and get() methods etc. This is actually encapsulation.

Encapsulation is the realization of the desired abstraction.

Happy learning !!

Leave a Reply

5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial