Abstraction in Java

1. Introduction

Abstraction is a concept of exposing only essential details and hiding implementation details. It is one of the essential OOPs concept apart from encapsulation, inheritance and polymorphism. Abstraction retains only information which is most relevant for the specific purpose.

For example:

ArrayList stores objects sequentially as list, and you can use the add() method to add elements to ArrayList, the remove() method to remove elements from it, and the get() method to retrieve elements from the ArrayList. That’s all you need to know to use ArrayList. That’s an abstraction in Java.

If you want to use ArrayList, you don’t need to understand how ArrayList works internally.

ArrayList uses abstraction by implementing List interface and List interface provides all necessary methods to use ArrayList.

so you can declare ArrayList as below:

and call add(), remove() and get methods of List interface.

Suppose you realized later that you need to use LinkedList rather than ArrayList as you need to perform more add and remove operations rather than access operations. You can go through ArrayList vs LinkedList to understand the differences between them.

You just need to change highlighted line.

ArrayList and LinkedList use abstraction by implementing a List interface. That’s why we could replace ArrayList with LinkedList in the above example.

Let’s take another real-life example.

When you search for any text on Google, you just type text in the text area and click on the search button. You might not be aware of what happens behind the scenes and how the google search algorithm works.

2. Ways to achieve abstraction

You can achieve abstraction using two ways in java.

  1. Abstract class(0 to 100% abstraction)
  2. Interface (100% abstraction)

2.1 Abstract Class in Java

An abstract class is the class which is declared abstract and can have abstract or non abstract methods.  It should be extended by child class and should implement abstract method.

Abstract method in java:
Abstract method is the method which do not have implementation i.e. it does not have any body.

2.2 Interface in Java

Interface is generally used to provide contract for class to implement. Interface do not have implementation of any method.A class implements an interface, thereby inheriting the abstract methods of the interface. So it is kind of signing a contract, you agree that if you implement this interface, then you have to use its methods.It is just a pattern, it can not do anything itself.

3. Abstraction example

Let’s say we have Sport as interface. Now, its implementation will be provided by classes called Cricket and Football. In a real scenario, the end user will not be aware of the implementation class, and the object of the implementation class can be provided by the factory method. Factory method can be used to create an object of implementation class based on some criterion.
Let’s create an interface called Sport.java:

Create a class named Cricket.java

Create a class named Football.java

Create a main class named SportInterfaceMain.java

When you run above program, you will get below output:

Playing cricket
=================
Playing football

4. Abstraction vs Encapsulation

Encapsulation is process of binding related data(variables and methods) into a class.We can achieve encapsulation using access modifers such as public, private, protected.

Encapsulation is implementation of desired abstraction.

Abstraction is more of design level concept and helps you to provide only essential details and hiding implementation details. We can achieve abstraction by abstract class and interface.

5. Conclusion

You have learnt about What is abstraction, how to achieve abstraction with help of example and difference between abstraction and encapsulation.
That’s all about abstraction in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *