Abstraction:

Abstraction refers to the hiding of the complex details and showing only necessary information. For example, when you turn your lights on, the room lights up. You just turn the light on. You do not need to know how did the electrons flow? What is going on in the wiring?

Abstraction can be achieved by two ways:

  • Abstract Classes.
  • Interfaces.

Abstraction with Abstract Class:

Abstract class is declared with abstract keyword. We will create an abstract class "Shape" with an abstract method "calculateArea()". We'll also create two concrete subclasses, Circle and Rectangle, that inherit from the Shape class and provide their own implementations of the calculateArea() method.

abstraction.java
// Abstract class representing a shape
abstract class Shape {
  // Abstract method for calculating the area
  public abstract double Area();
}
  
// Concrete subclass representing a circle
class Circle extends Shape {
    private double radius;
  
    public Circle(double radius) {
      this.radius = radius;
    }
  
    // Implementing the abstract method
    public double Area() {
    return Math.PI * radius * radius;
  }
}
  
// Concrete subclass representing a rectangle
class Rectangle extends Shape {
    private double length;
    private double width;
  
    public Rectangle(double l, double w) {
      this.length = l;
      this.width = w;
    }
  
    // Implementing the abstract method
    public double Area() {
    return length * width;
  }
}
  
public class Main {
  public static void main(String[] args) {
    // Creating objects of Circle and Rectangle Class
    Circle c = new Circle(5.0);
    Rectangle r = new Rectangle(4.0, 6.0);
  
    // Calculate and display areas
    System.out.println("Area: " + c.Area());
    System.out.println("Area: " + r.Area());
  }
}

Abstraction with Interfaces:

In this example, we'll use an interface called Sound to define a contract specifying a makeSound() method. We'll create two classes, Dog and Cat, that implement the Sound interface and provide their own makeSound() implementations.

abstraction.java
// Interface representing a sound
  interface Sound {
      void makeSound();
  }
  
  // Class representing a dog
  class Dog implements Sound {
      public void makeSound() {
          System.out.println("Dog barks.");
      }
  }
  
  // Class representing a cat
  class Cat implements Sound {
      public void makeSound() {
          System.out.println("Cat meows.");
      }
  }
  
  public class Main {
      public static void main(String[] args) {
          // Create objects of Dog and Cat
          Dog dog = new Dog();
          Cat cat = new Cat();
  
          // Make sounds
          dog.makeSound();
          cat.makeSound();
      }
  }

Benefits of Abstraction:

  • Code Reusability : Abstraction promotes code reusability by allowing multiple classes to implement the same interface or inherit from the same abstract class.
  • Modularity : It helps in breaking down complex systems into manageable and modular parts.
  • Encapsulation : Abstraction supports encapsulation by hiding implementation details and exposing a well-defined interface.
  • Maintainability : It enhances code maintainability by making it easier to modify or extend the system without affecting other parts of the code.

Abstraction is a fundamental concept in Java that helps to write organized and structured code for designing classes and interfaces. It enables us to creation of clean and understandable code, making it easier to work with complex systems.