What is an abstract class?-

An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete.

An abstract class in Java is a class that cannot be instantiated (i.e., you cannot create an object of an abstract class directly). It is often used as a base class for other classes. Abstract classes may contain abstract methods, which are methods that are declared without implementation in the abstract class. Subclasses that extend the abstract class must provide concrete implementations for all the abstract methods declared in the abstract class.

Key characteristics of an abstract class in Java include:

  1. Cannot be instantiated: You cannot create an object of an abstract class using the new keyword.
  2. May contain abstract methods: Abstract classes can have abstract methods (methods without a body) that must be implemented by concrete subclasses.
  3. Can have concrete methods: Abstract classes can also have fully implemented (concrete) methods that are inherited by subclasses.

Here’s an example:

java
abstract class Shape {
// Abstract method
public abstract double calculateArea();

// Concrete method
public void display() {
System.out.println("This is a shape.");
}
}

class Circle extends Shape {
private double radius;

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

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

class Main {
public static void main(String[] args) {
Circle circle = new Circle(5.0);
circle.display();
System.out.println("Area of the circle: " + circle.calculateArea());
}
}

In this example, Shape is an abstract class with an abstract method calculateArea() and a concrete method display(). The Circle class extends the Shape class and provides a concrete implementation of the calculateArea() method.