Java

What is Difference Between Abstract Class and Interface in Java

Java provides multiple ways to provide data abstraction, such as java interfaces and java abstract classes. Both interface and abstract class provide security by hiding the internal implementation details and only showing/highlighting the necessary details. However, there are numerous similarities and distinctions between abstract classes and interfaces.

This post will assist you in the below-listed aspects of Java interfaces and abstract classes:

  1. What is a Java Interface.
  2. What is a Java Abstract Class.
  3. Comparison Based on Similarities.
  4. Java Abstract Class Vs Java Interface.

So, let’s begin!

What is a Java Interface

It is a template for a class that contains some abstract methods and static/final variables. In java, interfaces provide only method declaration, while child/implemented classes provide the method definition.

What is a Java Abstract Class

Any java class that has at least one abstract(only method declaration) method is known as an abstract class in java.

Comparison Based on Similarities

Some important aspects of Java interfaces and abstract classes are listed below:

  • In java, we can’t create the object/instance of both interfaces or abstract classes.
  • Both provide only necessary details to the user.
  • Both provide abstraction.
  • Both can have abstract methods/functions.
  • Both hide the implementation information/details from the users.

Java Abstract Class Vs Java Interface

The “abstract” and “interface” keywords are used in java to create/declare the abstract classes, and interfaces respectively.

In interfaces, all members are considered public by default, so there is no need to specify the access modifier for the members of interfaces. While in abstract classes there is no such restriction and hence any access modifier can be used for the members of abstract classes such as public, protected, etc.

The basic syntax of an abstract class and an interface is shown in the below-given snippets:

Abstract Class

public abstract class ClassName{
public abstract void methodName();
}

Interface

public interface InterfaceName{
void methodName();
}

The interfaces must have all abstract methods, while the abstract classes can have both abstract and non-abstract methods.

The interface can have only static members, while abstract classes can have static and non-static members.

Interfaces have only “final”(unchangeable) members while abstract classes can have final and non-final members.

Interfaces don’t have any constructor while abstract classes have user-defined or default constructors.

Multiple inheritance can be achieved using interfaces while abstract classes don’t support multiple inheritance.

In java, the abstract class can inherit another class and can implement many interfaces, on the other hand, a Java interface can extend another interface only.

Example

The below-given example will let you understand how interfaces and abstract classes work in java:

interface FirstInterface {
    void methodOne();
    void methodTwo();
    void methodThree();
}
abstract class AbstractClass implements FirstInterface {
    public void methodThree() {
        System.out.println("Abstract class Implementing methodThree");
    }
}
class NormalClass extends AbstractClass {
    public void methodOne() {
        System.out.println("Implementing methodOne");
    }
    public void methodTwo() {
        System.out.println("Implementing methodTwo");
    }
    public void methodThree() {
        System.out.println("Implementing methodThree");
    }
}
public class InterfaceExample {
    public static void main(String[] args) {
        FirstInterface obj = new ClassThree();
        obj.methodOne();
        obj.methodTwo();
        obj.methodThree();
    }    
}

In this example, we created an interface “FirstInterface”, an abstract class “AbstractClass”, and two more normal Java classes and performed the following functionalities:

  • The FirstInterface contains three abstract methods.
  • The AbstractClass implements FirstInterface and overrides the methodThree().
  • The NormalClass extends the AbstractClass and and overrides the methodOne() and methodTwo() of FirstInterface.

The InterfaceExample is the main class from where we created the main method and invoked all the methods of FirstInterface:

Output verified the working of the above-given Java program.

Conclusion

In java, abstraction can be achieved using abstract classes and interfaces, and both of them can’t be instantiated. However, there are multiple distinctions between abstract classes and interfaces. For example, the interfaces must have all abstract methods while the abstract classes can have abstract and non-abstract methods; using interfaces, multiple inheritance can be achieved while abstract classes don’t support multiple inheritance, etc. This post explained various aspects of abstract classes and interfaces with the help of suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.