What is difference between interface and abstract class in Java

What is abstract class and interface in Java
The difference between abstract class and interface in Java is one of the tricky Java interview question and mostly appear 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 is 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 focus 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 abstract class is 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 difference between interface and abstract class in Java programming language and later we will see where to use abstract class and interface.

Though, if you are preparing for Java programming interview, you should also check out the Java Programming Interview Exposed, a great book specially designed to prepare for Java concepts based interview questions. It has many such questions from all important topics e.g. oop concepts, multithreading, collections, frameworks like Spring and Hibernate, unit testing, data structure and algorithm, coding, design pattern and modern technology questions like Android, Scala, and other JVM languages.

Difference between interface and abstract class in Java




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.

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


2) The second difference between interface and 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 abstract class. Even a class which doesn't contain any abstract method can be made abstract by using the abstract keyword.

3) 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. The Effective Java has one item dedicated to explaining about why you should be using interface for type declaration. You should check that out as well.

Difference between abstract class and interface in Java


4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because 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) Another notable difference between interface and abstract class is that when you add a new method in 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.

Here is a nice summary slide of highlighting key differences between an abstract class and interface in Java:
Difference between abstract class and interface in Java



That's all on the difference between abstract class and interface in Java, I will add more differences whenever I learn new things. As I said, in the first paragraph, after the introduction of default method in Java 8 (See Java 8 in Action) and the provision that you can have both static and default method inside an interface, the difference between abstract class and interface has become blur. Earlier, the interface only contains contract no implementation but now they can.


Other Java tutorials you may like
5 difference between Hashtable and HashMap in Java
What is Serialization in Java – Serializable interface
10 tough Core Java interview questions and answers
Top 5 thread questions and answers asked in Java Interviews
What is class file in Java - How to create Class in Java
10 Java Coding Interview questions and Answers for Java beginners.


16 comments:

  1. Great post - very informative. I found this page to be very helpful as well:

    http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/

    ReplyDelete
  2. Thanks dude really helpful ....

    ReplyDelete
  3. i lerned very well by IT??????????

    ReplyDelete
  4. Thank u... It s very useful for me.

    ReplyDelete
  5. i don't understand where we use interface and where we use abstract class ?

    ReplyDelete
    Replies
    1. Use abstract class if you want to provide a Skeleton class e.g. AbstractList class from java.util package, while use interface to define public API e.g. List is an interface and AbstractList is an abstract class. The List allows you to use List in a flexible way and AbstractList allows developer to create a new implementation of List easily.

      Delete
  6. 1. A class can implement more than one interface(Multiple Inheritance is possible with interface). But A class can not extend more than one abstract class(Multiple Inheritance is not possible with class).

    2. If You want to add a new method in interface, you need to implement that method in all the classes that implementing the interface. But if you want to add a new method in abstract class, you can add it as a non abstract method. You don't need to implement it in all the classes that extending the abstract class.

    3. Interface don't have constructors. But Abstract class has constructors.

    Prathap
    Java training in chennai

    ReplyDelete
  7. If we don't want to allow anybody to create the object to our class,then we should go for abstract class.
    Ex:HttpServlet.

    ReplyDelete
    Replies
    1. Indeed, but that's the half of the story, you want others to extend your class before use, that's the meaning of abstract class e.g. your servlet should extend HttpServlet and implement doGET(), doPost() methods etc.

      Delete
  8. This is all going to change in Java 1.8. the introduction of Functional interfaces changes the differences listed here :)

    ReplyDelete
    Replies
    1. @Anonymous, From Java 8 Onwards interface is not completely abstract i.e. they can also have code in form of static and default methods. That actually blur the difference between abstract class and interface but still you can implement multiple interface but only extend one class.

      Delete
  9. In an interface we cannot define static methods and can only define public static final
    variables

    ReplyDelete
    Replies
    1. @Anonymous, from Java 8 onward, interface can include static methods.

      Delete
  10. Typo found, there in the picture, should read: "To declare an interface, use interface keyword".

    ReplyDelete
    Replies
    1. @Aulo, good catch, thanks for pointing out, indeed "interface" is the keyword to declare interface in Java.

      Delete