Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we use interface extends interface

Tags:

java

interface

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.

like image 867
nikhil Avatar asked Jan 17 '23 15:01

nikhil


1 Answers

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.

like image 71
Sergey Kalinichenko Avatar answered Jan 20 '23 15:01

Sergey Kalinichenko