|
| 1 | +[`interfaces`][interfaces] are the primary means of [decoupling][wiki-loose-coupling] the uses of a class from its implementation. This decoupling provides flexibility for maintenance of the implementation and helps support type safe generic behavior. |
| 2 | + |
| 3 | +The syntax of an interface is similar to that of a class except that methods appear as the signature only and no body is provided. |
| 4 | + |
| 5 | +```java |
| 6 | +public interface Language { |
| 7 | + String speak(); |
| 8 | +} |
| 9 | + |
| 10 | +public interface ItalianLanguage extends Language { |
| 11 | + String speak(); |
| 12 | + String speakItalian(); |
| 13 | +} |
| 14 | + |
| 15 | +public interface ScriptConverter { |
| 16 | + void setVersion(String version); |
| 17 | + String getVersion(); |
| 18 | + String convertCyrillicToLatin(String cyrillic); |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +The implementing class must implement all operations defined by the interface. |
| 23 | + |
| 24 | +Interfaces typically do one or more of the following: |
| 25 | + |
| 26 | +- allow a number of different classes to be treated generically by the using code. In this case interfaces are playing the same role as a base class. An example of this is [java.io.InputStream][input-stream], |
| 27 | +- expose a subset of functionality for some specific purpose (such as [`Comparable<T>`][comparable]) or |
| 28 | +- expose the public API of a class so that multiple implementations can co-exist. One example is that of a [test double][wiki-test-double] |
| 29 | + |
| 30 | +```java |
| 31 | +public class ItalianTraveller implements ItalianLanguage { |
| 32 | + |
| 33 | + public String speak() { |
| 34 | + return "Ciao mondo"; |
| 35 | + } |
| 36 | + |
| 37 | + public String speakItalian() { |
| 38 | + return speak(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +public class ItalianTravellerV2 implements ItalianLanguage { |
| 43 | + |
| 44 | + public String speak() { |
| 45 | + return "migliorata - Ciao mondo"; |
| 46 | + } |
| 47 | + |
| 48 | + public String speakItalian() { |
| 49 | + return speak(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +public class FrenchTraveller implements Language { |
| 54 | + public String speak() { |
| 55 | + return "Ça va?"; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +public class RussianTraveller implements Language, ScriptConverter { |
| 60 | + |
| 61 | + private String version; |
| 62 | + |
| 63 | + public void setVersion(String version) { |
| 64 | + this.version = version; |
| 65 | + } |
| 66 | + |
| 67 | + public String getVersion() { |
| 68 | + return version; |
| 69 | + } |
| 70 | + |
| 71 | + public String speak() |
| 72 | + { |
| 73 | + return "Привет мир"; |
| 74 | + } |
| 75 | + |
| 76 | + public String convertCyrillicToLatin(String cyrillic) { |
| 77 | + throw new UnsupportedOperationException(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +public class DocumentTranslator implements ScriptConverter { |
| 82 | + |
| 83 | + private String version; |
| 84 | + |
| 85 | + public void setVersion(String version) { |
| 86 | + this.version = version; |
| 87 | + } |
| 88 | + |
| 89 | + public String getVersion() { |
| 90 | + return version; |
| 91 | + } |
| 92 | + |
| 93 | + public String translate(String russian) { |
| 94 | + throw new UnsupportedOperationException(); |
| 95 | + } |
| 96 | + |
| 97 | + public String convertCyrillicToLatin(String cyrillic) { |
| 98 | + throw new UnsupportedOperationException(); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Code which uses the above interfaces and classes can: |
| 104 | + |
| 105 | +- treat all speakers in the same way irrespective of language. |
| 106 | +- allow some subsystem handling script conversion to operate without caring about what specific types it is dealing with. |
| 107 | +- remain unaware of the changes to the italian speaker which is convenient if the class code and user code are maintained by different teams |
| 108 | + |
| 109 | +Interfaces are widely used to support testing as they allow for easy [mocking][so-mocking-interfaces]. |
| 110 | + |
| 111 | +Interfaces can extend other interfaces with the `extend` keyword. |
| 112 | + |
| 113 | +Members of an interface are public by default. |
| 114 | + |
| 115 | +Interfaces can contain nested types: `interfaces`, `enums` and `classes`. Here, the containing interfaces act as [namespaces][wiki-namespaces]. Nested types are accessed outside the interface by prefixing the interface name and using dot syntax to identify the member. |
| 116 | + |
| 117 | +By design, Java does not support multiple inheritance, but it facilitates a kind of multiple inheritance through interfaces. |
| 118 | + |
| 119 | +Moreover, the concept of [polymorphism can be implemented through interfaces][interface-polymorphism] underpins the interface mechanism. |
| 120 | + |
| 121 | +[interface-polymorphism]: https://www.cs.utexas.edu/~mitra/csSummer2013/cs312/lectures/interfaces.html |
| 122 | +[so-mocking-interfaces]: https://stackoverflow.com/a/9226437/96167 |
| 123 | +[comparable]: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html |
| 124 | +[wiki-test-double]: https://en.wikipedia.org/wiki/Test_double |
| 125 | +[wiki-polymorphism]: https://en.wikipedia.org/wiki/Polymorphism_(computer_science) |
| 126 | +[wiki-namespaces]: https://en.wikipedia.org/wiki/Namespace |
| 127 | +[wiki-loose-coupling]: https://en.wikipedia.org/wiki/Loose_coupling |
| 128 | +[interfaces]: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html |
| 129 | +[input-stream]: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html |
0 commit comments