1.

What is the difference between an interface and abstract class in Java?

Answer»

For classes and methods, the abstract keyword is a non-access MODIFIER. An abstract class is a special kind of class that can't be used to create objects (to access it, it must be inherited from another class). Talking about an abstract method, it has no body and it can only be used in an abstract class. The subclass provides the body (inherited from). 

In Java, an interface is a blueprint for a class. It features abstract methods and static constants. In Java, the interface is a means of achieving abstraction. The Java interface can only have abstract methods, not method bodies. In Java, it is used to achieve abstraction as well as MULTIPLE inheritances. To put it another way, interfaces can have abstract methods and variables. It isn't allowed to have a method body.

The differences between the two have been TABULATED below:

Abstract classInterface
Abstract classes can have abstract and non-abstract methods.Interfaces are allowed to have abstract methods only. Since Java 8, interfaces can also have default and static methods.
Abstract classes do not support multiple inheritances.Interfaces do support multiple inheritances.
Abstract classes can possess final, non-final, static and non-static variables.Interfaces can only possess static and final variables.
Abstract classes can provide the implementation of interfaces.Interfaces cannot provide the implementation of abstract classes.
The abstract keyword is used for declaring abstract classes.The interface keyword is used for declaring interfaces.
An abstract class can implement numerous Java interfaces by extending to another Java class.An interface is allowed to EXTEND to another Java interface only.
Java abstract classes are allowed to have class members like PRIVATE, protected, etc.Members of a Java interface cannot be private or protected.


Discussion

No Comment Found