Difference between Java abstract class and interface



Java abstract class Vs java interface

When to use java abstract class and when to use java interface is not only a natural question that comes to the mind when learning java abstract class and interface, but it's the question asked by the interviewers for jobs in java Object Oriented Programming (OOP) or even in other Object Oriented programming languages such as C# and C++. Thus, in order to know when to use abstract class instead of interface, you should first understand the difference between them.

Difference between interface and abstract class

What is the difference between java abstract class and java interface? The answer there is not only one difference but there are many differences. The next few paragraphs show the difference between the abstract class and the interface. I strongly recommend to understand them very well and not just read them. Let's enumerate the differences.

Abstract methods

The main difference is that all java interface methods are implicitly public and abstract which means they cannot have implementations but only the methods signature, see it live in Java Interface . While a java abstract class may have both abstract methods and non-abstract methods that implements a default behavior, which makes abstract class an ideal place to provide common or default functionality for more information see the example of Abstract class .

Variables

All Java interface Variables, if there are any, are by default static and final which means they are constants see the example of Java Interface . An abstract class may contain final and non-final variables, see it in the example of Abstract class .

Members access modifier

Members of a java interface are public by default. While java abstract class can have different access modifiers such as public, private and protected.

extends Vs implements

Java interface should be implemented using keyword implements, here is an example of Java Interface . While a java abstract class should be extended using keyword extends, see how it's done in the following example Abstract class . An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

Single inheritance and multiple inheritance

Java interface can extend multiple interface and a Java class can implement multiple interfaces which means multiple inheritance, but it can extend only one abstract class which means single inheritance. It's good to remind you that java does not permit multiple inheritance. The best way to achieve the multiple inheritance in java is through interfaces. Thus, an interface can provide more polymorphism support than an abstract class.

Instance of abstract class and interface

Neither java interface nor java abstract class can be instantiated. In others words, you can not create an instance of java interface or java abstract class. But the abstract class can be invoked if a main() exists.

Speed or performance myth

Some pretend that java abstract class is faster than java interface. I wrote and tested several programs to determine the performance of java abstract class and java interface. The results show that java interface is slightly slow with a difference of a few nanoseconds.

In order to implement interface in Java you need to provide implementation of all methods in each subclass or concrete class that inherits from the interface, while the abstract class may help you in this case by providing default implementation of non-abstract methods.

When to use java interface and when to use java abstract class is decided on what you want to achieve. This topic is discussed in the following article when to use abstract class and interface.

Here is a set of interesting java tutorials. Choose the tutorial you want to read either by using the following links or the ones on the left side of the current page.

I hope this can help.

Home| Abstract class part1| Abstract class part2| Java Interface part1| Java Interface part2| Abstract Class Vs Interface