In java, Abstract class and interface are very popular concept. Both are used to achieve the abstraction in java. In this article, we will see the difference between the abstract class and interface. There is a lot of difference between abstract class and interface in java.
Declaration and use
An interface is declared by the interface keyword. Subclasses use implements keyword to implement interfaces
An abstract class can have final, non-final, static, and non-static variables. But if you declare a final variable then you must initialize it otherwise the compiler will throw an exception at compile time.
An Interface has all methods are public and abstract by default. You can declare a method with the public, private, abstract, default, static and strictfp are permitted. If you try to use a protected modifier then it will throw an exception at compile time because it is not permitted the protected modifier. Since Java 8 we can define the default method and static method in the interface. In Java 9, we can define private methods also.
An abstract class is more flexible than the interface in declaring the methods. You can define abstract methods with protected modifier also. In the interface, the static method and default method is supported since Java 8 but in the abstract class, you could define the static and default method before java 8.
An abstract class can implement multiple interfaces. But it can’t extend more than one class.
Support multiple inheritances
As you know a class can implement multiple interfaces. So, it does support multiple inheritances because a class can inherit properties from different interfaces. In Java 8, default methods are also used to support multiple inheritances.
A class can extend only one class whether it is the concrete class or abstract class. So, it doesn’t support multiple inheritances.
When to use
An interface would be a good choice when you want to achieve 100% abstraction. It means when multiple classes want to provide different implementations to the only method signature.
An abstract class would be a good choice when you want to achieve 1% to 100% abstraction. It should be used when multiple classes want to provide different implementations and also want to share some common implementation.
An interface doesn’t contain any constructor. Because we can’t create an object of an interface. In abstract class, the constructor is used to initialize the variable of an abstract class. but the interface doesn’t contain any variable, It just contains the final variable.
An abstract class can have a constructor. By default, JVM provides the default constructor, you can also provide the constructor explicitly. We can’t create the object of the abstract class. The constructor can only be called during constructor chaining and used to initialize common variables, which are declared inside an abstract class.
Before java 8, An interface could not have the main method in the interface. But after Java 8, you can create the main method in the interface. because the main method is always using the static keyword. In Java 8, It allows declaring static methods also.