Abstraction in java

In this post, we will see about Abstraction in java.


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 in sequential manner as list and you can use add() method to add elements to ArrayList, remove() method to remove elements from it, and get() method to retrieve element from the ArrayList. That’s all you need to know to use ArrayList. That’s 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 List interface that’s why we are able to replace ArrayList with LinkedList in above example.

Let’s take another real life example.

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

Ways to achieve abstraction

You can achieve abstraction using two ways in java.

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

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.

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.

Abstraction example

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

Create class named Cricket.java

Create class named Football.java

Create main class named SportInterfaceMain.java

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

Playing cricket
Playing football

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.


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.

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, when a function is called by the Java compiler the task of correctly locating the method for the respective function is binding. Depending on when the compiler is […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.