Summary: In this tutorial, you will learn about abstract class and methods in Java with the help of the examples. Along with this, you will also learn about abstraction in Java.

Abstract Class in Java

In Java, an abstract class is a class that cannot be instantiated. That is, you cannot create new instances of an abstract class using the new operator.

Abstract classes are used to provide a common interface for a set of subclasses. They are useful when you want to define some behavior that you want all of the subclasses to have, but you don’t want to actually implement the behavior.

To create an abstract class in Java, you need to use the abstract keyword. Here is an example of how you can create an abstract class:

public abstract class Shape {
    // Abstract class definition goes here
}

An abstract class can have any number of methods, including both abstract methods and concrete methods. Abstract methods are methods that are declared in an abstract class, but do not have an implementation.

They are used to specify what behavior a subclass must implement. Concrete methods, on the other hand, are methods that have a complete implementation and can be called like any other method.

Here is an example of an abstract class with both abstract and concrete methods:

public abstract class Shape {
    // This is an abstract method
    public abstract double getArea();

    // This is a concrete method
    public void draw() {
        System.out.println("Drawing the shape...");
    }
}

Abstract Method in Java

An abstract method is a method that is declared in an abstract class, but does not have an implementation. That is, the method does not have a method body.

Instead, the abstract method is just a declaration that specifies the name, return type, and parameters of the method. Abstract methods are used to specify what behavior a subclass must implement.

Here is an example of how you can create an abstract method in an abstract class:

public abstract class Shape {
    // This is an abstract method
    public abstract double getArea();
}

In this example, the Shape class is an abstract class that has an abstract method called getArea(). The getArea() method does not have a method body. Instead, it is just a declaration that specifies the name, return type (double), and parameters (none) of the method.

How to use Abstract Class?

To use an abstract class, you need to create a subclass that extends the abstract class and implements any abstract methods declared in the abstract class. Here is an example of a subclass that extends the Shape abstract class:

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

public class Circle extends Shape {
    private double radius;

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

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

In this example, the Circle class extends the Shape abstract class and implements the getArea() method. The @Override annotation is used to indicate that the getArea() method is being overridden from the superclass.

Note that if you forget to include the @Override annotation and you make a mistake in the method signature, you will get a compile-time error. This is one of the benefits of using the @Override annotation: it helps you catch mistakes and ensures that your code is correct.

When to use Abstract Class or Method in Java?

In Java, abstract classes are used to define a common interface for a set of related subclasses. They are useful when you want to define some behavior that you want all of the subclasses to have, but you don’t want to actually implement the behavior.

Here are some situations where you might want to use an abstract class:

  1. When you want to share common behavior among subclasses: If you have a set of related subclasses that need to share some behavior, you can define that behavior in an abstract class and have the subclasses extend the abstract class.
  2. When you want to specify a requirement for subclasses: If you want to ensure that all subclasses of a certain class implement certain behavior, you can declare that behavior in an abstract class as an abstract method. This will force the subclasses to implement the method.
  3. When you want to prevent direct instantiation: If you don’t want clients of your class to be able to create instances of the class using the new operator, you can make the class abstract. This will prevent clients from creating new instances, but will still allow them to use the class as a type in their code.

Here is an example of an abstract class in Java that defines a common interface for a set of related subclasses:

public abstract class Shape {
    // This is an abstract method
    public abstract double getArea();

    // This is a concrete method
    public void draw() {
        System.out.println("Drawing the shape...");
    }
}

public class Circle extends Shape {
    private double radius;

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

    // This method is implemented to satisfy the abstract method in the superclass
    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle extends Shape {
    private double width;
    private double height;

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

    // This method is implemented to satisfy the abstract method in the superclass
    @Override
    public double getArea() {
        return width * height;
    }
}

In this example, the Shape class is an abstract class that has an abstract method called getArea() and a concrete method called draw().

The Circle and Rectangle classes are subclasses of the Shape class and implement the getArea() method to provide specific behavior for their respective shapes.

The draw() method is inherited from the Shape class and can be called on instances of the Circle and Rectangle classes.


Leave a Reply