When do we use interface extends interface

旧城冷巷雨未停 提交于 2019-12-19 08:54:27

问题


I wish to know when we can use an interface extending another interface. I wish to know a practical example and when we use it.


回答1:


You extend an interface when a subinterface provides everything the superinterface provides, and does something else of importance. For example, SortedMap<K,V> implements Map<K,V>, because sorted map is a map that supports all operations of a map, plus some operations applicable only to sorted maps.

This is similar to inheriting among classes, but it allows for multiple implementations. For example, one could implement a SortedMap as a sorted list of keys plus a parallel array of values, rather than a tree. This would let users swap in a faster or otherwise superior implementation without changing the rest of the code. In other words, inheritance among interfaces lets you preserve the benefits of programming to interfaces.




回答2:


Have a look at interfaces like java.util.Collection, java.util.Set to see how this is done, and how contracts can be tightened.




回答3:


  1. When we want to use multiple inheritance in our application then one interface should extends other interface.

  2. To make the parallel development of your application it is very necessary to write your code in such a way that you can incorporate newly discovered requirements into the existing code as painlessly as possible. So, if we implements the interface then concrete class names locks you into specific implementations, making down-the-line changes unnecessarily difficult. Therefore, we extends interface.



来源:https://stackoverflow.com/questions/10641277/when-do-we-use-interface-extends-interface

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