Abstraction in Java

Abstraction is a fundamental concept of object-oriented programming (OOP) in Java. It refers to the process of hiding implementation details and exposing only the necessary information to the user. In this tutorial, we will discuss what abstraction is in Java and how to implement it with examples.

Abstraction in Java

Abstraction is the process of hiding implementation details and exposing only the necessary information to the user. In Java, we can achieve abstraction through abstract classes and interfaces.

Abstract Classes

An abstract class is a class that cannot be instantiated, meaning we cannot create objects of it. Instead, we use it as a base class to create subclasses that can be instantiated. An abstract class may contain both abstract and non-abstract methods.

public abstract class Shape {
    private String color;
    
    public Shape(String color) {
        this.color = color;
    }
    
    public String getColor() {
        return color;
    }
    
    public abstract double getArea();
}

public class Circle extends Shape {
    private double radius;
    
    public Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }
    
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circle = new Circle("red", 5.0);
        System.out.println("Area of circle: " + circle.getArea());
        System.out.println("Color of circle: " + circle.getColor());
    }
}

In this example, we have defined an abstract class “Shape” that contains a constructor and two methods – “getColor” and “getArea”. The “getArea” method is declared as abstract, meaning that it does not have an implementation in the “Shape” class. We then create a subclass “Circle” that extends the “Shape” class and implements the “getArea” method. In the “Main” class, we create an object of the “Circle” class and call its methods.

Interfaces

An interface is a collection of abstract methods that can be implemented by a class. It defines a contract that the implementing class must follow. Interfaces are similar to abstract classes, but they cannot contain any non-abstract methods or instance variables.

public interface Vehicle {
    void start();
    void stop();
}

public class Car implements Vehicle {
    public void start() {
        System.out.println("Car is starting");
    }
    
    public void stop() {
        System.out.println("Car is stopping");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle car = new Car();
        car.start();
        car.stop();
    }
}

In this example, we have defined an interface “Vehicle” that contains two methods – “start” and “stop”. We then create a class “Car” that implements the “Vehicle” interface and provides an implementation for its methods. In the “Main” class, we create an object of the “Car” class and call its methods through the “Vehicle” interface.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author