Home >>Java Tutorial >Difference between abstract class and interface
Difference between abstract class and interface are discussed in this tutorial along with the exact use of these two elements of the Java language. In the Java Language the Abstract class and interface both are generally used in order to achieve abstraction where the users or programmers can declare the abstract methods. Please note that the abstract class and interface both of them can't be instantiated.
Apart from all these facts of these Java elements, there are various differences that are present between these two. Here is the list of the differences depicted below that are present between the abstract class and interface in Java language:
Abstract class | Interface |
---|---|
Abstract class in Java can possess abstract and non-abstract methods. | Interface in Java can only possess abstract methods. Since the release of Java 8, interfaces can possess default and static methods also. |
Abstract class are known to be non supportive to multiple inheritance. | Interface is known to support the multiple inheritance. |
Abstract class can also possess non-final, final, static and non-static variables. | Interface in Java only possess static and final variables. |
The implementation of interface is generally provided by the Abstract Class. | The implementation of abstract class cannot be delivered by the interface. |
The abstract keyword is basically used in order to declare the abstract class. | The interface keyword is basically used in order to declare interface. |
An abstract class can be used in order to extend another Java class and to implement the multiple Java interfaces. | An interface in Java can be used in order to extend another Java interface only. |
Just by using the keyword "extends", an abstract class can be extended. | Just by using the keyword "implements", an interface can be implemented. |
A Java abstract class can generally possess class members such as protected, private, etc. | Members of a Java interface are basically the public by default. |
Here is an example of the Abstract class: public abstract class Shape{ public abstract void draw(); } | Here is an example of the interface in Java: public interface Drawable{ void draw(); } |
Concrete method may be contained by the abstract class in Java language. | All of the methods of an interface in Java are abstract. |
In order to use an abstract class, the programmer needs to inherit it. If there are any abstract methods that are present then he should provide the body to them in order to override. | In order to use an interface the programmer needs to implement the interface and delivers body to (override) all the abstract methods of it. |
Members of an abstract class in the Java language are basically private, public, protected or default. | All the members of the interface in the Java are basically known to be either public or default. |
The programmer can consider using the abstract classes in case any of the following depicted statements apply to the situation:
The programmer can consider using the Interfaces in case any of the following depicted statements apply to the situation: