Mastering interfaces
By default, an interface is an abstract
construct. Before Java 8, all the methods in an interface were abstract
. In general, when you create an interface, you are defining a contract for what a class can do without saying anything about how the class will do it. A class signs the contract when it implements an interface. A class implementing an interface is agreeing to “obey” the contract defined in the interface. “Obeying” here means that, if a concrete (non-abstract) class is implementing an interface, the compiler will ensure that the class has implementation code for each abstract
method in the interface. As the Oracle tutorials state, “Implementing an interface allows a class to become more formal about the behavior it promises to provide.”
In contrast to classes, where you can (directly) inherit from only one other class, a class can implement many interfaces. Thus, interfaces enable multiple inheritance. Let&...