Difference between Abstract class and Interface in Java 8

Ever since JDK 8 has allowed concrete (non-abstract) methods on the interface like default and static methods, many of my readers have asked me how should they answer the classical abstract class vs interface questions. Earlier, an interface cannot have any concrete methods and that was the main difference between abstract class and interface but now that is not the case. In this post, I'll revisit this hugely popular Java interview question in light of Java 8 changes. As I said, before JDK 8, the level of abstraction was the clear-cut difference between abstract class and interface i.e. interface was the purest form of abstraction which only defines what interface is supposed to do without specifying how they should be implemented, in other words, it only declares API methods and leaves implementation to its subclasses.

But, the main difference between an abstract class and interface in Java 8 is the fact that an abstract class is a class and an interface is an interface.

A class can have a state which can be modified by non-abstract methods but an interface cannot have the state because they can't have instance variables.

The second difference is that an interface cannot have a constructor even in Java 8 but you may remember that abstract class can also have a constructor in Java.

All methods of an interface were abstract but since Java 8 you can define non-abstract methods in form of default and static methods inside interface in Java.

This brings many questions to come to mind.


Prima facia, in Java 8, an interface looks like an abstract class and one can reason about, can we use interface with default methods in place of an abstract class in Java?

Well, I believe they are for two different purposes and we will learn more once we start using Java 8 regularly, but following semantics difference between abstract class and interface with default method will guide you further :

1) Abstract classes are classes, so they are not restricted to other restrictions of the interface in Java e.g. abstract class can have the state, but you cannot have the state on the interface in Java.

2) Another semantic difference between interface with default methods and abstract class is that you can define constructors inside an abstract class, but you cannot define constructor inside interface in Java.


In reality, default or defender methods are introduced to maintain backward compatibility and same time making Collection API more suitable to be used inside key Java 8 features like lambda expressions.

Without adding default methods, it wasn't possible to declare any new method on existing interface in Java without breaking all classes which implement it, but because of default method, you can now better evolve your API.

They defend your code against implementing new methods hence they are also called defender methods. If you want to know more about default methods or new changes in Java 8 in general, I suggest you check out the What's New in Java 8 course on Pluralsight.

Difference between Abstract class and Interface  in Java 8 with Default Methods


Btw, you would need a Pluralsight membership to access this course, which costs around $29 monthly or $299 annually. I have one and I also suggest all developers have that plan because Pluralsight is like NetFlix for Software developers.

It has more than 5000+ good quality courses on all latest topics. Since we programmers have to learn new things every day, an investment of $299 USD is not bad.

Btw, it also offers a 10-day free trial without any obligation which allows you to watch 200 hours of content. You can watch this course for free by signing for that trial.


Though I certainly agree that difference between abstract class and the interface has reduced with the introduction of default methods, as well as allowing static methods inside interface and their usage will evolve once Java 8 become a mainstream Java development version, but you must remember that an abstract class is a class and an interface is an interface.

If you are designing API then use interface and if you are abstracting both state and behavior then use an abstract class.


Further Reading
Java SE 8 New Features
What's New in Java 8
The Complete Java MasterClass - Updated version


That's all about the difference between an abstract class and interface in Java 8. The key difference is that abstract class can maintain state but the interface cannot, and an abstract class can also have a constructor which is not allowed inside interface even in Java 8.


Other Java 8 Tutorials you may like
If you are interested in learning more about new features of Java 8, here are my earlier articles covering some of the important concepts of Java 8:
  • 5 Books to Learn Java 8 from Scratch (books)
  • How to join String in Java 8 (example)
  • How to use filter() method in Java 8 (tutorial)
  • How to format/parse the date with LocalDateTime in Java 8? (tutorial)
  • How to use Stream class in Java 8 (tutorial)
  • How to use forEach() method in Java 8 (example)
  • How to convert List to Map in Java 8 (solution)
  • 20 Examples of Date and Time in Java 8 (tutorial)
  • How to use peek() method in Java 8 (example)
  • How to sort the map by keys in Java 8? (example)
  • How to sort the may by values in Java 8? (example)
  • 10 examples of Optionals in Java 8? (example)
  • How to convert lambda expression to method reference in Java 8 (tutorial)
  • 5 Free Java 8 and  Java 9 courses for Programmers (courses)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

2 comments:

  1. What do you mean by "abstract class can have the state"?

    ReplyDelete
    Replies
    1. Hello Eugene, I mean abstract class can instance variable which can hold values.

      Delete