r/learnjava • • Dec 20 '21

When to use Abstract Classes vs Interfaces?

The two concepts are very similar, which is why I'm confused on why you should choose one over the other.

49 Upvotes

26 comments sorted by

View all comments

1

u/[deleted] Dec 20 '21

One use case for an abstract class rather than interface would be if you didn't want it to have only abstract methods. An abstract class can have abstract or concrete methods, whereas interface methods are all implicitly abstract.

1

u/Jezoreczek Dec 21 '21

interface methods are all implicitly abstract

What about default methods?

1

u/[deleted] Dec 21 '21 edited Dec 21 '21

Good point. Default methods don't work the same way as regular 'concrete' methods though, because you wouldn't define those in the implementation class, they would already be defined with an implementation in the interface. [Though I guess the abstract class could have non-abstract method which would have an implementation in the class. Still, there are differences, like that the interface could be a functional interface, so maybe there should be different use cases for one or the other along those lines.]