Java Abstraction

Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process for hiding the implementation details from the user. Java developers can only provide the functionality to the user and hide the implementation detail using abstraction.

Abstraction is one of the most important features of oops. It is achieved using abstract classes and interfaces. Java uses a keyword ‘abstract’ to make a class abstract.

Suppose a user is making a call to another user. He is simply enjoying the service, but he has no idea about the functionality of how things are working, this is called an Abstraction.

Let’s understand it with a programming example.

Suppose you are making a banking application and you are asked to collect all the information of the customer. Then there is a chance that you will come up with some additional info about the user like favorite movie, favorite Actor, but all these information are not necessary to create an application.

So you need to select only the useful information for your project from that collection of data. Since we can fetch/ remove/ select the information from that collection of data, this process is referred to Abstraction.

How to achieve Abstraction in Java?

There is two way to achieve abstraction in Java.

  • Java Abstract classes (0 to 100%)
  • Java Interfaces (100%)


This is an example of Java Abstraction.


Above example describes that how the abstraction achieved in java. Class Fortuner and method speed() is abstracted in the given example.

Java Abstract classes

An Abstract class is a class that contains abstract as well as concrete methods. A class is created Abstract using the keyword ‘abstract’.

An Abstract class can’t be instantiated, in other words. We cannot create the object of an abstract class.

Abstract class can have both abstract and concrete methods.

abstract class Account{

public abstract void debit();

public void credit()


System.out.println(“Account Statement”)}}

If we create the object of the given class Account then it will produce an error.

Account obj=new Account(); //it will produce an error

To use the abstract class, we must inherit it from the other class.

Abstract methods in abstract class do not have a body.

We can achieve 0 to 100 percent abstraction using Java abstract classes.

See the Example below


Java Interface

Java interface is a collection of abstract methods and static constants. It’s like a class that contains only static constants and abstract methods. We can achieve 100 % abstraction using Interfaces.

The interface does not contain any constructors. Methods in an interface are only public and abstract.

The interface is responsible for what a class will do; it is like a blueprint of the class.  Java uses interfaces to achieve multiple inheritances.

To use an interface in a class, use the keyword implements after the interface name. Java class can implement more than one interface (multiple interfaces).

Let’s take a tour with the following examples to understand the implementation of the interface.

Example 1

This is a format of the interface.

As you can see at the given example, an interface is declared using the interface keyword. Just like with classes, a Java interface can be declared public or package scope; it does not use access specifier.

The above example contains one variable and one method. In Java, the variable can be accessed directly from the interface, like this:

Example 2


Advantage of Abstraction

Some of the advantages of abstraction are as follows

  • It reduces view complexity of the class members.
  • It avoids duplication of code.
  • It increases the reusability of code.
  • It Increases the readability of the program if things are appropriately abstracted, then the reader does not retain everything.
  • It supports polymorphism.
  • It provides security.

Abstraction VS. Encapsulation

Both abstraction and interface are a major oops concept of Java. They allowed you to enhance the real world thing into an object so it can be implemented in the program. Many are got confused between abstraction and encapsulation because they are quite similar. Abstraction hides complexity while encapsulation hides internal working. Let’s have a look at some following differences.

1 Abstraction hides details at the design level. Encapsulation hides details at the implementation level.
2 Abstraction hides unwanted information for the user’s dynamic view. Encapsulation hides the block of code. Wrap it into a single unit.
3 It uses the keyword abstract to make an abstract class for abstraction In encapsulation create a class variable as private.
4 Abstraction is created using abstract classes and interfaces. Provides setter and getter for accessing and modifying the data.
5 Abstraction is used for hiding the backend data. It wraps up data into a single unit.


Pin It on Pinterest

Share This