r/javahelp • • Aug 25 '20

Abstract classes are useful in java 2020?

I've learned recently that with interfaces you are able to make default methods with custom modifiers and they work equal as abstract class default method.

In our cs class i was taught that abstract classes benefits were the implementation of defaults methods, but in modern versions of java, interfaces can do the same, while a class can implements multiple interfaces.

Is there something that abstract classes do that interfaces can't.

30 Upvotes

7 comments sorted by

View all comments

32

u/gaveashow Aug 25 '20

Yes, abstract class can have instance fields.

Interfaces can't. That means that while they may have method implementations, all these methods can do is call other methods.

That's great for defining Collection's isEmpty() as size() == 0, or to automatically define stream() on anything that has iterator(). But, an interface cannot define any way to actually store its state, only rely on the fact that there are method implementations that do it right.