Java Abstract Method

h‮:sptt‬//www.theitroad.com

In Java, an abstract method is a method that is declared but not implemented in an abstract class. The purpose of an abstract method is to define a method signature that must be implemented by any concrete subclass of the abstract class.

An abstract method is declared using the "abstract" keyword in its method signature, and it does not have an implementation in the abstract class. Instead, the implementation of the abstract method is provided by the concrete subclass that extends the abstract class.

Here is an example of an abstract method in Java:

public abstract class Shape {
    public abstract double getArea();
}

In this example, the Shape class has an abstract method called "getArea()", which does not have a method body. Any subclass of Shape that is not abstract must provide an implementation for this method. For example, a Circle subclass of Shape might look like this:

public class Circle extends Shape {
    private double radius;

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

    public double getArea() {
        return Math.PI * radius * radius;
    }
}

In this example, the Circle class extends the abstract Shape class and provides an implementation of the "getArea()" method by calculating the area of a circle using the radius field. By providing an implementation of the abstract method, the Circle class can be instantiated and used like any other class.