Abstraction in Java

Abstraction is a fundamental concept in computer programming and is a key principle of object-oriented programming. It refers to the process of representing essential features of an object or system without including the background details or implementation details. In this post, we will learn what is an Abstraction in Java in detail.

What is an abstraction?

We can say that abstraction is a process or mechanism to hide the internal detail and show only the functionality. We can take an example of a phone call. When we dial a receiver number and press the call button in our phone, the call goes to the receiver number. As a user, we don’t know how the call is going from one device from one location to another device in another location. We only know the functionality to dial a number and press the call button.

How can we implement abstraction in Java?

In Java, we can achieve abstraction using abstract classes and interfaces.

Using the abstract class in Java

An abstract class in Java is a class that is declared with an abstract keyword.

  • We can’t instantiate an abstract class. Normally, we use abstract class as a base class for one or more derived classes. An abstract class can have both abstract and concrete (non-abstract) methods, but it must include at least one abstract method.
  • We call an abstract method that is declared inside an abstract class or an interface, without having a body or implementation. Without implementing these abstract methods by their derived class or child classes they have no meaning.

Let’s see an example of an abstract class in Java:

abstract class Shape {
   // abstract method
   public abstract double area();
}

In the example above, the Shape class is an abstract class that has an abstract method called area(). This method must be implemented by any class that extends the Shape class.

Using an interface in Java

Interfaces are another way to achieve abstraction in Java. A class that implements an interface must implement all of the abstract methods in the interface. If we dont’ implement these method then we will see a compilation error. Unlike abstract classes, interfaces cannot have concrete methods and all of their methods are abstract. Here is an example of an interface in Java:

public interface Moveable {
   // abstract method
   public void move();
}

In this example, the Moveable interface has an abstract method called move() that must be implemented by any class that implements the Moveable interface.

After the releasee of Java version 8, we can have a non-abstract method in an Interface with the help of abstract keyword. We will learn this method in next post.

Abstraction is an important concept in Java because it allows developers to create flexible and reusable code. Developers can define a set of common behavior that multiple classes can inherit by using abstract classes and interfaces. While still allowing those classes to have their own unique implementation details. This helps to reduce code duplication and makes it easier to maintain and update the codebase.

Conclusion

Abstraction is a powerful technique for organizing and structuring code in Java. It allows developers to represent the essential features of an object or system without including unnecessary details, which leads to more flexible and reusable code.