This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

When to use: Java 8+ interface default vs. Abstract methods

Java 8 allows the Default implementation of Methods in an interface called Default Methods.

I am confused about when to use the interface default method instead of the abstract class (with abstract Method (s)).

So, when should you use the default method interface and when should you use an abstract class (with abstract methods)? In this case, are abstract classes still useful?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

There are many abstract classes beyond the default method implementation (such as private state), but starting with Java 8, whichever method you choose, you should default to use Defender (AKA) in your interface. Methods.

The limitation on default methods is that they can only be implemented in terms of calls to other interface methods without reference to the state of a particular implementation. Therefore, the primary use case is a higher-level convenience method.

The benefit of this new feature is that before you had to use abstract classes for convenience methods, thus limiting implementers to a single inheritance, you can now have the effort of really clean design programmers with interfaces and minimal implementation.

Default’s initial motivation for introducing methods to Java 8 was a desire to extend the Collections Framework interfaces using lambda-oriented approaches without breaking any existing implementations. Although this is more relevant to authors in public libraries, you may find the same functionality useful in your projects. You can add new conveniences in a centralized place without relying on the look and feel of other types of hierarchies.

Answer:

There are some technical differences. Abstract classes can still do more than Java 8 interfaces:

An abstract class can have a constructor. Abstract classes are more structured and can maintain a state.Copy the code

Conceptually, the main purpose of the Defender approach is backward compatibility after the introduction of new features in Java 8, such as lambda functions.

The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 1…

The top layer must be an abstract interface, such as ThreadPoolExecutor, which inherits the abstract abstractor AbstractExecutorService, which implements the ExecutorService interface

ThreadPoolExecutor cannot inherit AbstractExecutorService

So the designer designed the static inner class work in ThreadPoolExecutor, which inherits AQS, inner class-reject policy, and so on.


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️