Difference between Abstraction vs Encapsulation in Java

In very short
Encapsulation:– Information hiding.
and Abstraction:– Implementation hiding.

Abstraction vs Encapsulation

Abstraction and Encapsulation both are OOP concepts of any object oriented programming languages which allow us to wrap real world things into classes and objects for using in the programming of application. Both Abstraction and Encapsulation in OOPs using hiding information to the world, so these are very confusing to understand the difference between each other for beginners.

So in very simple word I am telling you about Abstraction, it is nothing but it is one of the OOP concept which focuses on the process of hiding the unwanted details and exposing only the essential features of a particular object or concept, and Encapsulation is also one of the OOP concepts which also hides data from outside world but there are key differences between them to way of hiding data outside of world.

So what is different way of hiding data, that is, Abstraction hides complexity by giving you a more abstract picture of a complex system, For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other, while Encapsulation hides internal working so that you can change it later, For example, all implementations and declaration related to the car wrap in the single unit called class i.e. Car class. In short we can say, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.

Suppose in above example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. When we first describe an object, you talk in more abstract term e.g. a Car has Engine, Gearbox, Steering objects but you don’t tell how to start engine, how to work Gearbox etc, Car will move, whether it will move by using tires or it will fly. It just moves. This is called Abstraction. We are talking about a most essential thing, which is moving, having engine, have gearbox etc rather than focusing on details of how to moving and starting engine.

In the programming, it is good practice to define different levels of Abstraction in the application and the classes should interact with other classes with the same level of abstraction or higher level of abstraction. As we increase the level of Abstraction, things start getting simpler and simpler because we leave out details.

In the OOP concept, Encapsulation is all about implementation. It wraps implementation and data into single unit, its purpose is to hide internal implementation of objects from outside world so that you can change this implementation later without impacting outside world.

Let’s see with example, in HashMap there are two very important method implementations put() and get() method. But developer only need to know hot put and get object to the HashMap instead how to work these implementation of HashMap internally. Because of Encapsulation, you can change the internal implementation of HashMap with ease without impacting clients who are using HashMap.

Advantage of Abstraction

  1. By using abstraction, we can separate the things that can be grouped to another type.
  2. Frequently changing properties and methods can be grouped to a separate type so that the main type need not under go changes. This adds strength to the OOAD principle -“Code should be open for Extension but closed for Modification”.
  3. Simplifies the representation of the domain models.

Advantage of Encapsulation

  1. Encapsulated Code is more flexible and easy to change with new requirements.
  2. Encapsulation makes unit testing easy.
  3. Encapsulation in Java allows you to control who can access what.
  4. It also helps to write immutable class in Java which are a good choice in multi-threading environment.
  5. Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thing are encapsulated in one place.
  6. Encapsulation allows you to change one part of code without affecting other part of code.

Difference between Abstraction vs Encapsulation

Difference between Abstraction vs Encapsulation in Java
  • Abstraction hide the things at design level but Encapsulation hide things at implementation level.
  • Abstraction can be defined as the process of hiding the unwanted details and exposing only the essential features of a particular object or concept, while Encapsulation is a way of wrapping up data and methods into a single unit. Encapsulation puts the data safe from the outside world by binding the data and codes into single unit.
  • Abstraction lets you focus on what the object does instead of how it does, while Encapsulation means hiding the internal details of how an object works. When you keep internal working details private, you can change it later with a better method.
  • Abstraction is supported using interface and abstract class while Encapsulation is supported using access modifiers e.g. public, private and protected.
  • Abstraction means to show What part of functionality while Encapsulation means to hide the How part of the functionality.

 

Previous
Next

One Response

  1. priya May 4, 2018