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

若如初见. 提交于 2019-12-02 01:18:01

I am not able to see any specific reason behind why this particular behavior is chosen by language designer.

Method definitions were in Java 1.0, Interface default methods where added in Java 8. Code written for Java 1.0 still needs to work.

Or its just that interface default method logically bears lesser importance than concrete implementation provided by the super class?

Effectively they have lesser importance being added after 20+ years of existing code being written.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!