5 Difference between Interface and Abstract class in Java? [Answer] (original) (raw)

What is abstract class and interface in Java?

The difference between abstract class and interface in Java is one of the tricky Java interview questions and mostly appears in core Java interviews. It has become now even trickier after Java 8 introduced default methods and allowed interfaces to have both default and static methods. Even though both interface and abstract class are a way to achieve abstraction in Java, there are significant differences between them, which you will learn in this article. Some time interviewer also not just focuses on key differences between abstract class and interface in Java but he is also interested in some practical experience e.g. when to use interface in Java and when to use an abstract class in Java.

This is actually the tricky part of this interview question and you must have a good understanding of what is an interface and abstract class in Java and how to use them. Anyway in this Java article, we will first see some syntactical differences between an interface and abstract class in Java programming language and later we will see where to use abstract class and interface.

However, if you are preparing for a Java programming interview, you should also check out these Java Interview Preparation Courses, a great collection of resources specially designed to prepare for Java concepts-based interview questions. It has many such questions from all important topics like oop concepts, multithreading, collections, generics, new Java features, Functional Programming, Lambda, Stream, and other core Java topics.

Abstract class vs Interface in Java

In the last section, we saw what is abstract class and interface and now let's see the difference between interface and abstract class in Java. While Java 8 enhancements which allow concrete methods like default and static method on the interface has reduced the difference between an interface and abstract class in Java, there is still some worth noting difference which every Java developer should be aware of.

1. Class vs Interface

The first and the major difference between an abstract class and an interface is that an abstract class is a class while the interface is an interface, which means by extending the abstract class you can not extend another class because Java does not support multiple inheritances but you can implement multiple inheritances in Java.

2. Non-Abstract Methods

The second difference between an interface and an abstract class in Java is that you can not create a non-abstract method in an interface, every method in an interface is by default abstract, but you can create a non-abstract method in the abstract class.

Even a class that doesn't contain any abstract method can be made abstract by using the abstract keyword.

The worth noting thing is here is that after Java 8 you can create non-abstract or concrete methods on interfaces like default and static methods but you cannot create a non-default and non-static method that is not abstract. You can learn more about Java 8 and other key Java features on a comprehensive course like The Complete Java Masterclass, which I highly recommend to every Java developer.

interface vs abstract class in Java

3. Usage

The third difference between abstract class vs interface in Java is that interface is better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective. Effective Java has one item dedicated to explaining why you should be using an interface for type declaration. You should check that out as well.

What is difference between interface and abstract class in Java

4. Method Resolution

The fourth difference between abstract class and interface in Java is that a_bstract classes are slightly faster than the interface_ because the interface involves a search before calling any overridden method in Java. This is not a significant difference in most of the cases but if you are writing a time-critical application then you may not want to leave any stone unturned.

5. Interface Evolution

Another notable difference between interface and abstract class is that when you add a new method in the existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good.

By using an abstract class you can provide a default implementation for a new method in the superclass without breaking existing clients.