About creating and using interfaces

An interface in object-oriented programming is like a class whose methods have been declared, but otherwise don't "do" anything. That is, an interface consists of "empty" methods.

One use of interfaces is to enforce a protocol between otherwise unrelated classes, as discussed next. For example, suppose you're part of a team of programmers, each of whom is working on a different part—that is, a different class—of a large application. Most of these classes are unrelated, but you still need a way for the different classes to communicate. That is, you need to define an interface, or communication protocol, that all the classes must adhere to.

One way to do this would be to create a Communication class that defines all of these methods, and then have each class extend, or inherit from, this superclass. But because the application consists of classes that are unrelated, it doesn't make sense to force them all into a common class hierarchy. A better solution is to create an interface that declares the methods these classes will use to communicate, and then have each class implement (provide its own definitions for) those methods.

You can usually program successfully without using interfaces. When used appropriately, however, interfaces can make the design of your applications more elegant, scalable, and maintainable.