Probably the most popular question during Java technical interviews is “What’s the difference between abstract classes and interfaces”. In my own three year old book I’ve offered the following answer:
“An abstract class may contain code in method bodies, which is not allowed in an interface.With abstract classes you have to inherit your class from the abstract one because Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.”
But starting from Java 8 this answer is wrong. This is how I’d answered it now:
1. The short answer: “Abstract classes can implement state in member variables, but interfaces can’t. You can extend from one abstract class, but implement multiple interfaces.”
2. The long one: “Both abstract classes and interfaces allow you to implement common static methods while leaving the implementation of some methods to other classes. Abstract classes can also have instance-specific methods and they enforce vertical inheritance hierarchy. Interfaces can’t have instance methods, but they don’t enforce any class hierarchy, so your class can extend any other class while implementing multiple interfaces.“
So abstract classes and interfaces have subtle differences now, which are good to recognize anyway. You can find more details on this subject in the draft of Chapter 5 of my upcoming Java for Kids book.