Abstraction in Java

In Java, abstraction captures only those details about a class that are relevant to the current context. For example, a java.util.Map stores key-value pairs and exposes two methods get() and put() to store and retrieve key-value pairs. This is, in fact, the only information we need if we want to use the Map in an application. How the Map works inside, we are not required to know it to use. This is very much an example of abstraction in Java.

Take a more real-life example of abstraction which can be a TV remote. You know that when you press any button on the remote, some function is applied on the television e.g. change the channel, change the volume level etc. You are not required to know how internally remote works, to use it properly. It is an example of abstraction.

1. Abstraction in OOP?

In object-oriented programming theory, abstraction involves defining objects representing abstract “actors” that can perform work, report on and change their state, and “communicate” with other objects in the system.

Abstraction in any programming language works in many ways. It can be seen from creating subroutines to defining interfaces for making low-level language calls.

Some abstractions try to limit the breadth of concepts a programmer needs, by completely hiding the abstractions they in turn are built on, e.g. design patterns.

2. Types of abstraction

Typically abstraction can be seen in two ways:

  • Data abstraction
  • Control abstraction

2.1. Data Abstraction

Data abstraction is the way to create complex data types and expose only meaningful operations to interact with the data type while hiding all the implementation details from outside works.

The benefit of this approach involves the capability of improving the implementation over time e.g. solving performance issues if any. The idea is that such changes are not supposed to impact client code since they involve no difference in abstract behavior.

2.2. Control Abstraction

Any software consists of numerous statements written in any programming language. Most of the time, statements are similar and repeated over places multiple times.

Control abstraction is the process of identifying all such statements and exposing them as a unit of work. We normally use this feature when we create a function to perform any work.

3. How to Achieve Abstraction in Java?

As abstraction is one of the core principles of Object-oriented programming practices and Java follows all OOPs principles, abstraction is one of the major building blocks of java language.

In Java, abstraction is achieved by interfaces and abstract classes. Interfaces allow you to abstract the implementation completely, while abstract classes allow partial abstraction as well.

  • Data abstraction spans from creating simple data objects to complex collection implementations such as HashMap or HashSet.
  • Similarly, control abstraction can be seen from defining simple function calls to complete open-source frameworks. control abstraction is the main force behind structured programming.

3.1. Abstraction using Interfaces

Let’s see one more example of abstraction in Java using interfaces. In this example, I am creating various reports which can be run on demand at any time during the application’s lifetime. As a consumer of the report, a class needs not to know the internals of the report’s run(), it only should execute this method and the report will be executed.

public interface Report {

    List<Object> run(ReportContext reportContext);
}

public class ReportContext {
	//fields
}
public class EmployeeReport implements Report {

    @Override
    public List<Object> run(ReportContext reportContext) {
    
        //Custom Logic
        System.out.println("Executing employee report");
        return null;
    }
}
public class SalaryReport implements Report {

    @Override
    public List<Object> run(ReportContext reportContext) {
        //Custom logic
        System.out.println("Executing salary report");
        return null;
    }
}

Now execute the reports with run() method.

public class Main {
    public static void main(String[] args) {

        ReportContext reportContext = new ReportContext();
        //Populate context

        Report eReport = new EmployeeReport();
        eReport.run(reportContext);

        Report sReport = new SalaryReport();
        sReport.run(reportContext);
    }
}

Program output.

Executing employee report
Executing salary report

4. Difference between Encapsulation and Abstraction

Encapsulation is realization of the desired abstraction.

Abstraction is more about hiding the implementation details. In Java, abstraction is achieved through abstract classes and interfaces.

Encapsulation is about wrapping the implementation (code) and the data it manipulates (variables) within the same class. A Java class, where all instance variables are private, and only the methods within the class can manipulate those variables, is an example of an encapsulated class.

If you want to read more about abstract classes and interfaces in Java, follow my next post Exploring interfaces and abstract classes in java.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode