What are the differences between abstract class and interface in Java?

The main difference between abstract class and interface is driven by abstract methods. An abstract class may or may not have abstract methods, while all methods declared inside an interface must be abstract (an abstract method is a method that is declared without an implementation -- without braces, and followed by a semicolon). Abstract classes and interfaces have their own application uses, and cannot be used interchangeably. Here, a tabular comparison is presented between abstract classes and interfaces.

Table 1: Differences between abstract class and interface in Java
Abstract Class Interface

An abstract class is a class that is declared abstract. It may or may not have abstract methods.

An interface in Java is implicitly abstract and adding that modifier is considered redundant and makes no difference.

An abstract class can have both abstract and non-abstract methods. Non-abstract methods in an abstract class are used to implement default behavior.

Methods declared in an interface are by default abstract and public; therefore, they cannot have implementation. It means to say that an interface cannot contain non-abstract methods.

Non-abstract methods of an abstract class can be declared static because to call a non-abstract static method there is no need to create an instance of the class. Of course, a method cannot be declared abstract and static both in abstract class.

On the other hand, interface methods must not be static because all the methods of an interface are implicitly abstract, and an abstract method can never be declared static.

An abstract class can declare instance variables as well, along with constants.

All variables defined in an interface must be public, static, and final - in other words, interfaces can declare only constants, not instance variables.

Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.

Interfaces do not have constructors. Interfaces are not part of an object's inheritance tree.

Members of an abstract class can be public, private or protected depending upon the required visibility level.

All members of an interface are by default public.

An abstract class is extended using keyword extends.

An interface is implemented using keyword implements.

An abstract class can extend another class and implement one or more interfaces.

An interface can extend one or more other interfaces. An interface cannot extend anything but another interface.

When to Use Abstract Class Instead of Interface

When to use abstract class: In some situations, the superclass does not directly relate to a "thing" in the real world, and because of this we do not instantiate the superclass. For example, all four and two-wheelers are vehicles but there is no real world object as "vehicle" it is a conceptual entity, which has no real world existence. In that case we can declare an abstract class called "Vehicle" and place all common attributes and functionalities inside that class. Later we can create a subclass "Car" which inherits all common attributes and methods from "Vehicle". Now an object of class "Car" can be created and it can be assigned to the reference of "Vehicle".

In above explained situation we should use abstract class rather than interface because this subclass-superclass relationship is genuinely an "is a" relationship.

When to use interface: On the other hand we interfaces should be used when a class promises some behaviors to provide. Interfaces form a contract between the class and the outside world. Moreover, iterface should be used when the subclass needs to inherit from another class.

Hope you have enjoyed reading difference between abstract class and interface in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.