Abstraction in Java

Last Updated : 8 Jan 2026

In Java, abstraction is one of the important concepts of object-oriented programming system. Abstraction is used to hide implementation details and showing only essential features to the user. In this chapter, we will learn about abstraction, how to achieve it with different approaches.

What is Abstraction in Java?

features or capabilities that are necessary to the user. It emphasizes what an object does rather than how it does it.

The following are the important features of abstraction in Java:

  • Abstraction shows only the essential functionality while hiding complex implementation details.
  • Abstraction allows the use of abstract classes and interfaces to define methods without implementation.
  • Changes in implementation do not affect the external code that uses the abstraction.
  • Abstraction restricts direct access to internal details.
  • Abstraction promotes loose coupling that makes the system easier to scale and maintain.
  • Abstraction helps avoid code duplication by defining common behavior in a single place.

Understanding Abstraction with Real-World Examples

The following real-world examples help explain this concept clearly:

  • ATM Machine:
    When we use an ATM, we interact with a simple interface such as entering a PIN, selecting a transaction, and withdrawing cash. The complex internal processes like user authentication, transaction verification, and communication with bank servers remain hidden from the user. This is a practical example of abstraction.
  • Car Driving:
    While driving a car, we control it using the steering wheel, accelerator, and brakes without understanding the internal working of the engine, transmission, or braking system. The car abstracts these complexities, allowing the driver to focus only on driving.

Achieving Abstraction in Java

In Java, abstraction can be achieved in the following two ways:

  1. Using Abstract Class (For Partial Abstraction): Abstract class provides partial abstraction by allowing both abstract and implemented methods.
  2. Using Interface (For 100% abstraction): Interface provides 100% abstraction by allowing only method declarations, which must be implemented by classes.

Let's understand the both approaches to achieve abstraction in details.

1. Abstraction Using Abstract Class

Abstraction can be achieved using an abstract class that may contain abstract methods (without a body) as well as concrete methods (with implementation). The abstract methods define what needs to be done, while subclasses provide how it should be done by implementing those methods.

Syntax

The following syntax shows how abstraction is implemented in Java using an abstract class with abstract and non-abstract methods:

Example of Abstraction Using Abstract Class

The following example demonstrates abstraction using an abstract class, where the Animal class defines an abstract method and a concrete method, and the Dog class provides the implementation of the abstract method.

Execute Now

Output:

Bark bark
I can eat.

2. Abstraction Using Interface

In Java, another way to implement abstraction is by using interfaces. Interfaces are useful for achieving 100% abstraction. In Java, as well as in other languages, interfaces include both variables and methods, but do not provide a method body.

Therefore, subclasses must implement every method of the interface. Multiple inheritance is also possible through interfaces. Note that the methods of an interface are public and abstract by default.

Syntax

In the following code snippet, the Bike class implements the Drivable interface and also includes the drive() method.

Example of Abstraction Using Interface

The following example demonstrates abstraction using an interface, where the Person interface declares a method and different classes (Student and Lecturer) provide their own implementations of that method.

Execute Now

Output:

This is the display method of the student class
This is the display method of the lecturer class

Advantages of Abstraction

The following list shows the advantages of abstraction in Java.

  • By hiding the implementation details, abstraction makes complex systems easier to comprehend.
  • Abstraction maintains various components of the system separated.
  • Maintenance of code is done more efficiently using abstraction.
  • Abstraction enhances security by only displaying the details that are necessary to the user.

Disadvantages of Abstraction

The following list shows the disadvantages of abstraction in Java.

  • If overused, abstraction can add unnecessary complexity.
  • Flexibility in implementation may be reduced using abstraction.
  • Abstraction can make understanding the system and debugging harder for unfamiliar users.
  • Performance can be affected by the layers of abstraction.

Common Mistakes to Avoid

The following is a list of common errors that can occur while working with abstraction, which should be avoided:

  • Ensure that in the concrete subclass, abstract methods are implemented.
  • Avoid making everything abstract when it is not required. Use abstraction only when it enhances the design.
  • When abstract methods are overridden, ensure the method signature matches exactly; otherwise, it can cause errors.

Next TopicREPL in Java