Abstraction

Abstraction is one of the four pillars of object-oriented programming (OOP). It is the process of hiding the implementation details from the user and only showing the essential functionality. This makes the code more modular and reusable, and it also makes it easier to understand and maintain. It allows you to define a blueprint for a class without providing a complete implementation.

In Java, abstraction can be achieved using abstract classes and interfaces.

1. Abstract Classes

With abstract classes in Java allows you to define a common interface (method signatures) for a group of related classes while leaving the implementation details to the concrete subclasses. Abstract classes serve as a blueprint for creating more specialized classes.

Use the abstract keyword to declare an abstract class. An abstract class can have both abstract methods (without implementation) and concrete methods (with implementation).

// Abstract class
abstract class Shape {
    // Abstract method (no implementation)
    abstract double area();
}

// Concrete subclass 1
class Circle extends Shape {
    double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    // Implementing the abstract method area() for Circle
    double area() {
        return Math.PI * radius * radius;
    }
}

// Concrete subclass 2
class Rectangle extends Shape {
    double length;
    double width;

    Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    // Implementing the abstract method area() for Rectangle
    double area() {
        return length * width;
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating objects of concrete subclasses
        Circle circle = new Circle(5.0);
        Rectangle rectangle = new Rectangle(4.0, 6.0);

        // Calling the area() method on objects of different concrete subclasses
        double circleArea = circle.area();
        double rectangleArea = rectangle.area();

        // Displaying the results
        System.out.println("Area of Circle: " + circleArea);
        System.out.println("Area of Rectangle: " + rectangleArea);
    }
}

This example demonstrates how abstraction with abstract classes allows you to define a common interface (area() method) for related classes (Circle and Rectangle) while letting each subclass provide its own implementation of the method.

2. Interface

An interface is a special type of abstract class that only contains abstract methods. Interfaces cannot contain any fields or constructors. It is a collection of abstract methods (methods without a body) that define a contract for classes that implement the interface. When a class implements an interface, it is obligated to provide concrete (non-abstract) implementations for all the methods declared in that interface.

// Define an interface named "Shape"
interface Shape {
    // Abstract method declarations (no method body)
    double area(); // Calculate the area of the shape
    double perimeter(); // Calculate the perimeter of the shape
}

// Implement the interface in a class
class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }
}

class Rectangle implements Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double area() {
        return length * width;
    }

    @Override
    public double perimeter() {
        return 2 * (length + width);
    }
}

public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(5.0);
        Rectangle rectangle = new Rectangle(4.0, 3.0);

        // Calculate and display the area and perimeter of the shapes
        System.out.println("Circle Area: " + circle.area());
        System.out.println("Circle Perimeter: " + circle.perimeter());

        System.out.println("Rectangle Area: " + rectangle.area());
        System.out.println("Rectangle Perimeter: " + rectangle.perimeter());
    }
}

In the Main class, we create instances of Circle and Rectangle, and then we call the area() and perimeter() methods on these instances to calculate and display the area and perimeter of the shapes.

By using an interface, we achieve abstraction, as we define a common contract (the Shape interface) that multiple classes can adhere to while providing their own implementations for the methods. This allows for code flexibility and modularity in your Java programs.


Abstraction enables you to create well-organized and modular software systems by separating the interface from the implementation, allowing for flexibility and extensibility in your code.

Last updated