What is the difference between an interface and abstract class?

前端 未结 30 1769
情歌与酒
情歌与酒 2020-11-21 11:51

What exactly is the difference between an interface and abstract class?

30条回答
  •  有刺的猬
    2020-11-21 12:13

    The only difference is that one can participate in multiple inheritance and other cannot.

    The definition of an interface has changed over time. Do you think an interface just has method declarations only and are just contracts? What about static final variables and what about default definitions after Java 8?

    Interfaces were introduced to Java because of the diamond problem with multiple inheritance and that's what they actually intend to do.

    Interfaces are the constructs that were created to get away with the multiple inheritance problem and can have abstract methods, default definitions and static final variables.

    See Why does Java allow static final variables in interfaces when they are only intended to be contracts?.

提交回复
热议问题