The abstraction design pattern is an essential part of object-oriented programming. In other words, it allows us to define an interface and then derive smaller and smaller implementations of the interface until only what we need remains. This blog post aims to explore Java's built-in abstraction mechanisms and how to use them effectively.

What is Abstraction in Java?

Abstraction is the process of hiding the implementation of the application from the user and presenting only functionalities.

Alternatively, data abstraction can refer to identifying only those characteristics of an object that are necessary while ignoring the ones that are irrelevant. The properties and behaviors of an object help to differentiate it from similar objects and help to classify/group them.

As another example, it hides the internal details of sending SMS so that only the essentials are visible to the user, such as typing the text and sending it. As far as message delivery is concerned, you do not know how it is processed internally.

Rather than focusing on how an object works, abstraction lets you focus on what it does.

Abstraction Example in java

Let’s consider the following example for implementing Abstraction in java.

abstract class Bike
    abstract void accelerate(); 

//concrete class
class Gusto extends Bike
    void accelerate(){
        System.out.println("Gusto::Start");
   
    }
}
class Main{
    public static void main(String args[]){ 
        Bike obj = new Gusto();   
        obj.accelerate();         
    }  
}

Output:

Gusto::Start

write your code here: Coding Playground

What Is Java Abstract Class?

The Java programming language implements abstractions through abstract classes and interfaces. Firstly, let's take a look at abstract classes. Abstract classes are those that get declared with the keyword "abstract" and cannot get instantiated.

There may or may not be abstract methods in an abstract class (methods without implementation). Abstract classes, in the JVM, are incomplete classes without a complete behavior.

Below is a general syntax for abstract classes:

abstract class <classname>
    {
            public abstract void abstractMethod();
            public void normalMethod()
            {
                    //function body
            }  
    }   

The syntax above shows that abstract classes can have both abstract and non-abstract methods. A class declaration begins with the word 'abstract'.

Types of Abstraction in Java

There are two types of abstraction:

Data Abstraction

The process Data abstraction is one of the most popular abstractions for creating complex HashMap or HashSet data types while hiding their implementation details and showing only meaningful and needed an operation for user interaction.

This approach helps solve performance issues and also improves the implementation time. Changes made during performance improvements do not affect client code.

Control Abstraction

An abstraction process contains identifying all similar and repetitive statements and exposing them as a single unit. This approach is helpful when a function needs to perform a given task.

Advantages of Abstraction in Java

  • It simplifies the process of viewing things.
  • Maximizes Code duplication and software reuse by using loosely coupled classes.
  • This feature enhances the security and confidentiality of an application by exposing only necessary details to the user.
  • As classes become easy to understand, maintenance is simplified, and debugging is easier without causing harm to other modules.

Data Encapsulation vs Data Abstraction in Java

  • In a sense, encapsulation is an extension of abstraction.
  • Abstraction is the process of hiding the implementation details while data encapsulation is the process of hiding data.
  • Data abstraction involves showing the external details of an entity and hiding its implementation details, whereas encapsulation binds data members and methods together.
  • Encapsulation hides parts of data, while abstraction provides access to them.

Conclusion

Abstraction is a fundamental and popular concept in computer science. There are a few types of abstraction, each with its own definition. The essence of abstraction is to hide unnecessary details from a programmer and provide a clean and simple interface to achieve a specific task.