When to use: Java 8+ interface default method, vs. abstract method

后端 未结 15 1956
闹比i
闹比i 2020-11-22 07:01

Java 8 allows for default implementation of methods in interfaces called Default Methods.

I am confused between when would I use that sort of interface default

15条回答
  •  情歌与酒
    2020-11-22 07:40

    As described in this article,

    Abstract classes versus interfaces in Java 8

    After introducing Default Method, it seems that interfaces and abstract classes are same. However, they are still different concept in Java 8.

    Abstract class can define constructor. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.

提交回复
热议问题