Abstract classes and interfaces are used for abstraction in Java. Abstraction in object-oriented programming refers to the hiding of implementation details from the end users.

In abstraction, you can know what are the functionalities, but you can’t know how they were implemented.

Let’s look at each of them and try to understand why they are used.

Abstract Class

A class that cannot be instantiated as an object and may or may not have abstract methods in it is termed an abstract class in Java. An abstract method is a method that does not have an implementation body when declared.

<img alt="abstract class example" data- data-src="https://kirelos.com/wp-content/uploads/2023/03/echo/geekflareAbstract.jpg" data- decoding="async" src="data:image/svg xml,” width=”800″>
Example of an abstract class GraphicObject – Oracle

You can create an abstract class by specifying the abstract keyword before the class keyword.

abstract class abstractClass {
    void run() {
        System.out.println("ran");
    }
}

An abstract class can be extended by other classes. In other words, it can be subclassed as well.

abstract class AbstractClass {
    void run() {
        System.out.println("ran");
    }
}

class ExtendingAbstractClass extends AbstractClass {
    void newMethod() {
        System.out.println("new");
    }

    @Override
    void run() {
        System.out.println("override");
    }
}

Abstract classes are used to implement common methods among multiple classes which extend a given abstract class. Also, the ability to define abstract methods inside abstract classes makes them incredibly useful to classes that have similar methods but with different implementations. Let’s take an example.

Consider a car that has some functionalities, such as start, stop, reverse, etc. These functionalities are common among all types of cars.

But what about automation functionalities such as self-driving? The implementation of those functionalities can differ for different types of cars. Let’s see how you can create an object-oriented program related to it.

First of all, create a Car class that will be extended by multiple classes of different car types.

abstract class Car {
    void start() {
        // implementation
        System.out.println("runs car");
    }

    void stop() {
        // implementation
        System.out.println("engine stops");
    }

    void reverse() {
        // implementation
        System.out.println("reverse mode enabled");
    }

    abstract void selfDrive();
}

The method start(), stop(), and reverse() are methods that are common in all cars. So their implementation is already defined inside the Car class itself. However, a certain type of car can have different implementations of self-driving mode. And thus, you can define selfDrive() as an abstract method and implement it in different ways in different classes of different types of cars.

class CarTypeA extends Car {
    @Override
    void start() {
        super.start();
    }

    @Override
    void stop() {
        super.stop();
    }

    @Override
    void reverse() {
        super.reverse();
    }

    void selfDrive() {
        // custom implementation
        System.out.println("Type A self driving mode enabled");
    }
}
class CarTypeB extends Car {
    // ...all similar methods

    void selfDrive() {
        // custom implementation
        // different implementation than CarTypeB
        System.out.println("Type B self driving mode enabled");
    }
}

It’s important to note that, if a subclass does not implement all abstract methods defined in the abstract class, then it should be declared as an abstract class itself.

Interface

An interface is a way to tell a class what methods must be implemented by it. For example, if you consider the example of a car, it has some basic functions. It can start, move and stop. These functions are common in all cars.

So, if you implement an interface of a car in a class, you must implement all of the methods for the car to function properly and safely.

Similar to abstract classes, we cannot instantiate or create objects of an interface. It can be considered a fully abstract class because it contains only abstract methods i.e. methods without an implementation body.

You can create an interface by using the interface keyword.

interface CAR {
    void start();
    void stop();
    void move();
}

Implement an interface by using the implements keyword when defining a class.

class CarTypeB implements CAR {
    public void start() {
        System.out.println("started");
    }

    public void stop() {
        System.out.println("stopped");
    }

    public void move() {
        System.out.println("running");
    }
}

Similarity

No instantiation as an object is the one thing that abstract classes and interfaces have in common.

Differences

Abstract Class Interface
Inheritance & Implementation Only one abstract class can be inherited by a class. Multiple interfaces can be implemented by a class.
Variable Types It can have final, non-final, static, and non-static variables. It can have only static and final variables.
Method Types It can contain both abstract as well as non-abstract methods. It can only contain abstract methods, but static methods are an exception.
Access Modifiers An abstract class can have an access modifier. The method signatures defined in the interface are public by default. An interface doesn’t have an access modifier.
Constructors & Destructors It can declare constructors and destructors. It can’t declare constructors or destructors.
Speed Fast Slow
Differences between abstract class & interface

When to use abstract class and interface?

Use abstract classes when:

  • You want to share some common methods and fields among multiple classes.
  • Declaring non-static and non-final fields in order to modify the state of the object to which they are bound.

You can use interfaces when:

  • You want to define the behavior of a class that implements the interface, but you don’t care about the way in which it’s implemented.
  • You want to make sure that a class implements all the methods to function properly.

Final Words

Interfaces are mainly used to create APIs because they can provide a structure to implement functionality without worrying about the actual implementation.

Abstract classes are generally used to share common abstract and non-abstract methods among multiple classes, which extend the abstract class to make the code more reusable.

Learn more about Java with the help of these online courses for java. Are you preparing for an interview on Java? Here are some interview questions on Object Oriented Programming.