Home >>Java Tutorial >Difference between abstract class and interface

Difference between abstract class and interface

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.

Correct use of Abstract class and Interface in Java language

The programmer can consider using the abstract classes in case any of the following depicted statements apply to the situation:

  • There are some related classes in the Java applications that need to share some lines of the code then the user can put these lines of code within an abstract class and this abstract class must be extended by all these related classes that are present.
  • The user can also define non-static or non-final field(s) in the abstract class; hence, by a method the user can basically access and modify the state of the Object to which they belong.
  • The programmer can basically expect that the classes that are used to extend an abstract class possess many common methods or fields, or even require access modifiers other than the public.

The programmer can consider using the Interfaces in case any of the following depicted statements apply to the situation:

  • As a matter of fact we known that it is total abstraction and all the methods that are declared within an interface should be implemented mandatorily by the class(es) that generally implements this interface.
  • A class in Java can basically implement more than one interface and it is known as multiple inheritance.
  • If the programmer wants to specify the behavior of a particular data type but he/she is not concerned about who is basically implementing its behavior.

No Sidebar ads