Abstraction in Java with an example

Abstraction is a fundamental principle of Java object-oriented programming (OOP). It is the practice of concealing the complexity of an object's implementation and only exposing a simplified interface through which other objects can interact. In other words, abstraction enables us to concentrate on the essential characteristics of an object while ignoring its implementation details.

We can use abstract classes and interfaces to implement abstraction in Java.

An abstract class is one that cannot be instantiated and is intended to serve as a base class from which other classes can inherit. It may contain one or more declared but undefined abstract methods that must be implemented by any class that inherits from it. Abstract classes are used to define shared behavior between related classes while allowing each class to implement its own distinct functionality.

To declare an abstract class in Java, use the "abstract" keyword. Here is the general form:

public abstract class ClassName {
    // abstract methods and variables
}

An interface is a collection of abstract methods that define a contract for an object's behavior. It only contains method signatures and no implementation. A class can implement multiple interfaces, allowing it to behave like multiple objects. Interfaces are used to ensure consistency across different classes with similar behavior without requiring them to share the same base class.

To declare an abstract method in Java, use the "abstract" keyword again. Here is the general form:

public abstract void methodName();

Java's keyword "public" makes classes and methods available to the entire program, "abstract" indicates that a class or method is unfinished and must be implemented by a subclass, "class" declares a class, and "void" indicates that a method does not return a value.

Java abstraction example

Consider the following program as an example demonstrating the abstraction in Java.

Java Code
// Abstract class Vehicle
abstract class Vehicle {
  // Private instance variables brand and year
  private String brand;
  private int year;

  // Constructor to set brand and year
  public Vehicle(String brand, int year) {
    this.brand = brand;
    this.year = year;
  }

  // Getter methods to access brand and year
  public String getBrand() {
    return brand;
  }

  public int getYear() {
    return year;
  }

  // Abstract method start(), which is not implemented in this class
  abstract void start();
}

// Concrete class Car, which extends the abstract class Vehicle
class Car extends Vehicle {
  // Constructor to set brand and year using the superclass constructor
  public Car(String brand, int year) {
    super(brand, year);
  }

  // Implementation of the abstract method start()
  void start() {
    System.out.println("Starting the " + getBrand() + " car.");
  }
}

// Concrete class Motorcycle, which extends the abstract class Vehicle
class Motorcycle extends Vehicle {
  // Constructor to set brand and year using the superclass constructor
  public Motorcycle(String brand, int year) {
    super(brand, year);
  }

  // Implementation of the abstract method start()
  void start() {
    System.out.println("Starting the " + getBrand() + " motorcycle.");
  }
}

// Main class AbstractionExample
public class AbstractionExample {
  public static void main(String[] args) {
    // Create instances of Car and Motorcycle
    Vehicle car = new Car("Ford", 2023);
    Vehicle motorcycle = new Motorcycle("Harley Davidson", 2022);

    // Call the start() method on each object, without knowing the specific implementation details
    car.start();
    motorcycle.start();
  }
}
Output
Starting the Ford car.
Starting the Harley Davidson motorcycle.

The main method in the AbstractionExample class creates instances of the Car and Motorcycle classes and calls their start() methods. The start() method is called on the Vehicle objects without knowing the specific details of how the method is implemented in each subclass.

This demonstrates the concept of abstraction, where we can define a common interface for objects (in this case, the Vehicle class with its start() method), without needing to know the specific implementation details of each individual object.

Abstraction is when an object's implementation details are kept secret while only a minimal interface is made available to other objects. Methods that are not implemented in an abstract class but are expected to be implemented by subclasses are called abstract. Hiding implementation details and defining a standard interface for objects are two ways in which abstraction facilitates code reuse, maintainability, and modularity.

Through the use of abstraction, we are able to create code that is portable across implementations of an interface or abstract class. Through the use of abstraction, complex systems can be designed with fewer moving parts that can be independently implemented and tested.

Advantages of abstraction in Java

Disadvantages of abstraction in Java

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!