Last Updated:

The Difference Between an Abstract Class and an Interface in Java

Difference Between an Abstract Class and an Interface in Java

In this article, we'll learn about the difference between an abstract class and an interface, and when it's better to use an interface and when it's an abstract class.

The Difference Between an Abstract Class and an Interface

  1. The keyword is used to create an abstract class with methods, and the keyword is used to create an interface and cannot be used with methods.abstract interface
  2. Subclasses use a keyword to inherit an abstract class. At the same time, they must ensure the implementation of all claimed methods in an abstract class. If a subclass is not an abstract class and uses a keyword to implement interfaces, it must provide an implementation for all methods declared in the interface.extendsimplements
  3. Abstract classes can have methods with implementations, whereas an interface provides absolute abstraction and cannot have any implementations of a method.
  4. Abstract classes can have constructors; interfaces cannot have constructors.
  5. An abstract class has all the features of a regular Java class, except that we can't instantiate it. We can use a keyword to create an abstract class. An interface can only have static public static final constants and method declarations.abstract
  6. Abstract class methods can have public, private, protected, and static access modifiers, and interface methods can implicitly have public and abstract, so we can't use other access modifiers with interface methods.
  7. A subclass can inherit only one abstract class, and implement multiple interfaces.
  8. An abstract class can inherit from another class and implement interfaces, and an interface can only inherit from other interfaces.
  9. We can run an abstract class if it has a method We can't start the interface because it can't have an implementation of the main() method.main().
  10. An interface provides a so-called contract for all classes that implement an interface. Abstract Classes is a class with a default method implementation for subclasses.

That's the difference between an interface and an abstract class. Now we can move on to find out when it is better to use the interface and when the abstract class.

Interface or abstract class?

The choice between an interface and an abstract class depends on many factors. Let's look at the main arguments in favor of interfaces and abstract classes.

  1. Java does not support multiple inheritance, so each class can inherit only one superclass. In this case, any class can implement several interfaces. That is why interfaces are the best choice for your project. Also, writing code using interfaces is one of the best practices of Programming in Java.
  2. If you decide to write a lot of methods, the best solution for you is an abstract class. The thing is that we can provide a default implementation for some methods that are common to all subclasses.
  3. If we want to add or remove a method from the interface, then there may be problems with this. The thing is, we can't declare additional methods in an interface without changing all the classes that implement that interface.

Use Interfaces and Abstract Classes

In most cases, using interfaces with abstract classes will be the best solution for any project. For example, the JDK has java.util.List, an interface that contains many methods. There is also an abstract class java.util.AbstractList, which is the backbone for all methods of the List interface. This means that any subclass can inherit from that class and implement only the methods it needs.

Programmers should always start with the interface as the foundation of the system and define the methods that each subclass should implement. We can also create a new (child) interface that inherits the parent interface, but with certain methods. In this case, a class that does not need all the methods of the base interface can implement a child interface. Subclasses now have the ability to choose between a base interface and a child interface.

Changes in Java 8 interfaces

Starting with Java 8, it became possible to write the implementation of methods in interfaces. We can create default methods as well as static methods in interfaces and enforce them.