When we use the extends interface

I want to know when we can use an interface that extends another interface. I want to know a practical example and when we use it.

+4
source share
3 answers

You extend the interface when the subinterface provides everything that the superinterface provides, and does something else important. For example, SortedMap<K,V> implements Map<K,V> , because a sorted map is a map that supports all display operations, as well as some operations that apply only to sorted maps.

This is similar to inheritance between classes, but allows for many implementations. For example, you can implement SortedMap as a sorted list of keys plus a parallel array of values, rather than a tree. This will allow users to perform faster or more efficient implementations without changing the rest of the code. In other words, inheritance between interfaces allows you to preserve the benefits of programming for interfaces.

+7
source

Look at interfaces like java.util.Collection , java.util.Set to find out how this is done and how contracts can be compressed.

+2
source
  • When we want to use multiple inheritance in our application, one interface should extend another interface.

  • To make parallel development of your application, it is very necessary to write your code in such a way that you can introduce new discovered requirements into existing code as painlessly as possible. Thus, if we implement the interface, then specific class names block you in specific implementations, which makes unnecessary changes without changes. Therefore, we are expanding the interface.

0
source

Source: https://habr.com/ru/post/1413043/


All Articles