Multiple Inheritance Ambiguity with Interface

前端 未结 6 2210
轮回少年
轮回少年 2020-12-24 12:48

We all know about the diamond problem regarding multiple inheritance -

   A
  / \\
 B   C
  \\ / 
   D

This problem describe an ambiguous

6条回答
  •  半阙折子戏
    2020-12-24 13:47

    To add to existing answers about Java8 multiple inheritance with interfaces (a.k.a. how Java still avoids the diamond problem):

    There are three rules to follow:

    1. A class always wins. Class's own method implementation takes priority over default methods in Interfaces.

    2. If class doesn't have any: the most specific interface wins

    1. If above is not the case, inheriting class has to explicitly state which method implementation it's using (otherwise it won't compile)

提交回复
热议问题