Core JAVA

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

  • 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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago