Significance of inheriting method from superclass instead of default method from implementing interface in java 8

前端 未结 2 704
傲寒
傲寒 2021-01-20 17:05

I came across following paragraph while reading about java 8 default methods from here:

If any class in the hierarchy has a method with same signature

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 17:51

    Answer is simple: you are allowed to implement multiple interface but you can just extend one class. Indeed you can't have multiple default methods with the same name by implementing multiple interfaces.

    If in any circumstance a default method has higher priority than a method implemented inside a real class in the hierarchy you end up having the same exact problem for which Java designers didn't want multiple inheritance: ambiguity.

    As a personal opinion: a default method is seen as a "implementation of a functionality when none is defined", if the method is defined in a real class it is expected to be a more specific implementation.

提交回复
热议问题